For elements in the s and p blocks, the "magic number" of electrons is 8. Why 8? 2 electrons fit in the s orbitals, and 6 electrons fit in the p orbitals. In total, 8 valence electrons completely fill both the s and p orbitals, leading to a very stable system. This set of 8 electrons is called an "octet". Elements in the s and p blocks will react until they have eight electrons around them (bonds count as two electrons, lone pairs count as two electrons). This leads to the greatest level of stability for the molecule. As a result, oxygen is most stable when it has two bonds and two sets of lone pairs (total: 8). Carbon is most stable when it has 4 bonds and no lone pairs (total: 8). Exceptions: B and Al only have 3 valence electrons, so they can only form 3 bonds with their own electrons. They are often are stable at 6 valence electrons (3 bonds). However, they will form a fourth bond if possible (only if they receive an entire lone pair from the other reactant). Elements after period 3 have empty d orbitals that can also accept electrons to form bonds, and thus can exceed the octet rule to have up to 12 electrons. This is common with transition metal complexes, noble gases like Xe or Rn, and halogens like Br and I.