Atoms bond to reach a full outer shell. There are two main ways. In an ionic bond one atom hands electrons to another: sodium gives its one valence electron to chlorine, leaving Na⁺ and Cl⁻, and the opposite charges hold them together. Ionic bonds form between a metal and a nonmetal, and ionic compounds are usually hard, high-melting crystals like salt.
In a covalent bond two atoms share a pair of electrons. This happens between nonmetals. Hydrogen makes one bond, oxygen two, nitrogen three and carbon four: that's why water is H₂O, ammonia NH₃ and methane CH₄. Atoms can share two pairs (a double bond, as in O₂ and CO₂) or three (a triple bond, as in N₂). More shared pairs make a shorter, stronger bond.
When the two atoms in a bond have different electronegativities, the electrons are shared unevenly and the bond is polar: the oxygen end of an O–H bond is slightly negative (δ−) and the hydrogen slightly positive (δ+). A molecule with polar bonds is polar overall only if they don't cancel. Water is bent, so it's polar; carbon dioxide is straight, O=C=O, so its two polar bonds point opposite ways and cancel.
To write an ionic formula, balance the charges. Magnesium forms Mg²⁺ and chlorine Cl⁻, so you need two chlorides for each magnesium: MgCl₂. Aluminum forms Al³⁺ and oxygen O²⁻; the smallest whole numbers that balance are two of each to three: Al₂O₃ (2 × +3 = +6, 3 × −2 = −6).
Metals have a third kind of bonding: their valence electrons drift freely through the whole lump of metal, which is why metals conduct electricity and heat.
Practice
Which pair is most likely to form an ionic bond?
More info
Quick reference
Ionic bond
Electrons transferred; metal + nonmetal.
Covalent bond
A shared pair of electrons; nonmetals.
Double / triple bond
Two / three shared pairs.
Polar bond
Uneven sharing: δ+ and δ− ends.
Ionic formula
Smallest whole numbers that make the charges add to zero.
The other chemistry and chemical engineering courses