explain why some atoms gain or lose electrons to form ions