Atoms generally form bonding arrangements that give them filled shells of electrons like a noble gas configuration. The stability the atom aims for is that of its nearest noble gas.
For example, Lithium (Z = 3, Electronic Configuration = 1s2, 2s1) of the second row would prefer losing one electron to become Li+ (Z = 3, Electronic Configuration = 1s2) having an electronic arrangement similar to its nearest noble gas Helium (Z = 2, Electronic Configuration = 1s2).

However, for Fluorine (Z = 9, Electronic Configuration = 1s2 2s2 2p5) it would prefer to accept an electron to reach its nearest inert gas Neon (Z = 10, Electronic Configuration = 1s2 2s2 2p6) in its electronic configuration.

The noble gas configuration for second-row elements is eight valence electrons in the atom’s outermost shell.
There are exceptions to the octet rule. An atom may have an incomplete octet or exceed the octet number and still form bonds. Examples of such molecules are NO, BCl3, and PF5.
Read More – Octet Rule (Chapter – Bonding in Atoms, Part of Organic Chemistry Fundamentals Course)