When atoms catching electrons (pnictogens, chalcogens or halogens) are brought in contact with atoms loosing electrons (alkaline metals, alkaline earth metals or the boron group), electrons will be exchanged:
The ions produced by this exchange attract each othe by their opposite charges and agglutinate to ionic lattices: