Ions are formed when atoms gain or lose electrons to achieve a stable electron configuration. There are two main types of ions: cations, which are positively charged and form when metals lose electrons, and anions, which are negatively charged and form when nonmetals gain electrons. Cations have fewer electrons than protons, while anions have more electrons than protons. Examples given are sodium forming the Na+ cation by losing one electron, and oxygen forming the O2- anion by gaining two electrons.