Transition metals show variable valency and these elements have d-orbital as penultimate orbital and the outermost orbital is the s-orbital. Now, the atomic number of Fe, Iron is 26 and the electronic configuration is (1s)2 (2s)2 (2p)6 (3s)2 (3p)6 (4s)2 (3d)6 (aufbau's principle). But the exact arrangement is also possible with (1s)2 (2s)2 (2p)6 (3s)2 (3p)6 (3d)6 (4s)2. Therefore, because of the different electronic configurations which the element can have it shows variable valency. Another reason is that an atom has to complete 2 or 8 electrons in its outermost shell therefore, when Fe or any other transition element reacts with the other element then these transition atoms share the electrons according to their requirements.
An atom of an element can sometimes lose more electrons than are present in its valence shell, i.e. Loss from penultimate shell, and hence exhibit more than one or variable valency. Eg: 1. Cu 1+ & Cu 2+ 2. Ag 1+ & Ag 2+ 3. Hg 1+ & Hg 2+ 4. Au 1+ & Au 3+ 5. Fe 2+ & Fe 3+ 6. Pb 2+ & Pb 4+ 7. Sn 2+ & Sn 4+ 8. Pt 2+ & Pt 4+ 9.Mn 2+ & Mn 4+
It depends upon the element upon how many electrons it loses or gains.