Positive and negative ions are formed by gaining or losing electrons from neutral atoms. Metallic elements produce positively charged ions by losing electrons while nonmetallic elements produce negatively charged ions by gaining electrons.
Inside the atom of every element are sub-particles. A proton carries a positive charge, an electron carries a negative charge and a neutron does not carry any charge. In a neutral atom, the number of protons equals the number of electrons, thus neutralizing all charges. However, atoms have the tendency to either lose or gain electrons to stabilize their electronic structures, which only occurs when their highest energy level is fully occupied or empty of electrons.
Due to the small number of electrons present in the outermost energy level of metals, these elements are inclined to give up their electrons to attain stability. When electrons are removed from a neutral atom, the ionization potential energy is expended and a positively charged metal ion is produced, referred to as a "cation." In the case of nonmetals, which only require one or two electrons to fill out their highest energy level, these elements tend to accept electrons. The neutral atom releases the electron affinity energy to attract electrons and produce a negatively charged nonmetal ion called an "anion." When metal ions combine with nonmetal ions, an ionic compound is formed, which is held together by strong electrostatic forces called ionic bonds.