Ions are formed when atoms gain or lose electrons in order to achieve a more stable electron configuration. This can happen due to chemical reactions, electrical interactions, or other means. Atoms tend to lose or gain electrons so as to achieve a full outer shell of electrons, as this is energetically more favorable.