The process by which an atom or molecule gains or loses electrons, resulting in the formation of ions.