The octet rule dictates that atoms are most stable when their valence shell attains the electronic configuration of the nearest noble gas.
Example: Oxygen (atomic number 8)
The electronic configuration of oxygen is 2, 6.
The nearest noble gas is neon (atomic number 10) with an electronic configuration of 2, 8.
An atom of oxygen tends to gain two electrons to obtain the stable configuration of 2, 8.