The outside shell represents the valence electrons. These outer electrons are the ones involved in chemical reactions. The metals in the first 2 groups lose their electrons easily in chemical reactions so that a stable octet (8) of electrons in the "shell" below is now exposed. The halogens gain an electron because they already have 7 in the outer shell and need one more to make the stable octet. So you see, the gaining and loss of electrons generally leads toward more stability when the ions form. That's why you don't normally find sodium and chlorine hanging around; they are too unstable. But you do see lots of sodium chloride (salt), the ionic form of the atoms just mentioned.