Metals tend to lose electrons. This is due to metals having low ionization energy. An atom's ionization energy is the minimum energy it requires for the removal of one of its electrons.
On a periodic table, ionization energy increase as one moves from left to right across a row. Elements found on the left side of this table have much lower ionization energies than those on the right. Likewise, elements on the right side tend to gain electrons. These elements are generally nonmetals.
In reactions between metals and nonmetals, the number of electrons lost by a metal will depend in which group it is in. For example, metals in group 1A will lose one electron from its valence shell, while a metal in group 3A loses three electrons. Alternatively, nonmetals in group 5A gain three electrons because they have five electrons in the valence shell.