We will use the term shell rather than energy level but either is acceptable. The maximum number of electrons in each shell, going from the middle to the outside, is 2, 8, 8, 18. Below is a picture of a potassium atom. Its electron structure is 2, 8, 8, 1. An atom that has the maximum number of electrons in its outer shell will be stable. Atom B shares one of its 7 outermost electrons with atom A, so atom A then has 8 electrons in its outermost shell. They are then fairly stable, although not as stable as sodium chloride. Two chlorine atoms share electrons, but sodium gives an electron. This makes sodium chloride much more stable than chlorine gas.