octet rule
Why do atoms bond at all? A simple, surprisingly useful answer is that main-group atoms tend to gain, lose or share electrons until they are surrounded by eight valence electrons — the same stable count found in the noble gases neon, argon and so on. This is the octet rule, and it is the rule of thumb behind most of the Lewis structures you will ever draw.
Eight is special because a full s and p set in a shell holds two plus six equals eight electrons (one s and three p orbitals). An atom can reach an octet by transferring electrons (sodium loses one to look like neon, chlorine gains one to look like argon — that is ionic bonding) or by sharing them (two chlorines share a pair so each counts eight — that is covalent bonding). Hydrogen is the standard exception: it is happy with just two electrons, a filled 1s, a duet rather than an octet.
The octet rule works beautifully for the second-period elements carbon, nitrogen, oxygen and fluorine, which is why it is taught first. But inorganic chemistry is full of honest exceptions: electron-deficient atoms such as boron in BF3 settle for six; hypervalent atoms such as sulfur in SF6 or phosphorus in PF5 are surrounded by more than eight; and odd-electron molecules such as NO and NO2 have an unpaired electron and cannot pair everything up. Treat the octet rule as a strong default, not a law of nature.
In CO2, carbon forms two double bonds, one to each oxygen; counting the shared electrons, carbon sees eight and each oxygen sees eight, so every atom obeys the octet rule and the molecule is a textbook success of the idea.
Carbon dioxide: a clean octet on every atom.
The octet rule is a guideline, not a law; it fails routinely for boron and beryllium (too few electrons), for third-period and heavier centres (more than eight), and for any molecule with an odd number of electrons.