We figure most atoms want to be happy. The idea behind happy atoms is that atomic shells like to be full. If you are an atom, and you have a shell, you want your shell full. Some atoms have too many electrons, one or two extra. These atoms like to give up their electrons.
Some atoms are really close to having a full shell.
These atoms go around looking for other atoms who want to give up an electron. Anyway, you can see that Sodium and Magnesium have a couple of extra electrons. They, like all atoms, want to be happy. They have two possibilities. The way chemistry works, it's easier to give up a few electrons.