Metals and nonmetals......
Instead, how about we consider the fact that metals and nonmetals don't actual lose or gain electrons as they form compounds? The common oversimplification presented in most beginning chemistry classes is that bonds are either "ionic" or "covalent", and that in the "ionic" variety electrons are "transferred" from metal to nonmetal, and in the "covalent" variety the electrons are "shared." That line of thinking creates a bonding dichotomy where bonds have to be completely one or the other.
But in reality there are few (if any) completely ionic bonds. If there are no completely ionic bonds, then there is no complete "transfer" of electrons. Suppose that "ionic" and "covalent" represent the ideal extremes, but real bonds lie along a bonding continuum between those two hypothetical extremes. Real chemical bonds have characteristics of both ionic bonds and covalent bonds in varying degrees depending on the electronegativity (*) difference, ΔEN. Although the term "electronegativity" had been around since the 19th century, Linus Pauling was the first to develop a useful scale of the electronegativities of the elements. He also suggested a way to estimate the percent ionic character in a bond based on the electronegativity difference.
............. percent ionic character = 100(1 - e^(-ΔEN²/4))
What we find is that bonds between the alkali metals and the halogens have the most polar bonds (highest percent ionic character) and therefore are the most ionic. But even at that NaCl, a network of sodium and chlorine atoms through the entire sample has bonds with about 70% ionic character (30% covalent character). The closer two elements are on the periodic table the lower the percent ionic character. Therefore, there are many metal-nonmetal compounds with bonds with higher covalent character that ionic character. And the bottom line is that in these and even in the case of NaCl there is no complete transfer of electrons from metal to nonmetal.
But there are cases where transfers do occur and that is when compounds dissolve in a polar solvent like water. When NaCl dissolves the transfer of an electron is completed to form two free ions, one with a +1 charge and one with a -1 charge. Even some compounds with high covalent character ionize to form charged free ions. The classic example is HCl which produces H+ ions and Cl- ions even though HCl gas exists as discrete molecules with bonds which have only about 20% ionic character.
Therefore, the "loss" and "gain" of electrons takes place, not in compound formation, but in free ion formation, where the more electronegative elements acquires an electron from the less electronegative element. It is also important to recognize that there are limits to the charges on free ions in aqueous solution. The limit is essentially -1 for the elements with the greater electronegativity, like the halogens. It's easy to see why. A great deal of energy would be needed to "push" a negatively charged electron onto a negatively charged ion. That is why there are no "O^2-" or "P^3-" ions in solution despite what might be suggested by the "octet rule". The octet rule is not what its cracked up to be, and it is perfectly ok to completely ignore the octet rule. Metal ions rarely have positive charges of more than +3 in aqueous solution. Sn^4+ is one of the few exceptions.
It is extremely important not to confuse the oxidation state of an element in a compound or polyatomic ion with an actual charge on the element. For instance, manganese in the permanganate ion, MnO4^-, has an oxidation state of +7, but by no means as it lost seven electrons, nor does it have a charge of +7. In fact, the bonds between Mn and O in MnO4^- have high covalent character.
* Definition of electronegativity == a measure of the tendency of an element in a compound to attract shared pairs of electrons.