To understand the reason why the octet is considered more stable you have to understand what binds electrons to atoms in the first place, which is the attraction between the negatively charged electron, and the positively charged nucleus. You can think of electrons in lower shells being more strongly attracted to the nucleus, because they are closer to it. The octet rule works well for elements in the second and third row of the periodic table, because for these elements there can be a maximum of 8 electrons in their shells.
Consider Oxygen, which as a neutral atom has six electrons in it's outermost (valence) shell. Since this is an electronegative element, it tends to gain electrons whenever it's in an ionic bond. How many electrons? Well, we can give oxygen two extra electrons before filling it's shell. If we try to give it three extra electrons, the third one has to go into the next highest shell, which isn't strongly attracted to the nucleus. Since the attraction of the third electron would be so weak, usually oxygen only pulls two electrons, and "forms an octet".
Consider Magnesium, which as a neutral atom has two electrons in it's outermost (valence) shell. Mg, being a metal, tends to give up it's electrons in an ionic bond. How many electrons? The two electrons in the outermost shell are pretty weakly bound, so they can be given up easily. If you try to take a third electron, it must come from the next lower shell, which is bound much more tightly. So, Mg usually only gives up two electrons, and also "forms an octet".