Ionic compounds are generally formed between a metal and a non-metal. This is because metals tend to lose electrons easily, while non-metals tend to gain electrons. When a metal and a non-metal react, the metal atoms lose electrons to the non-metal atoms, resulting in the formation of positive and negative ions, respectively. These ions are then attracted to each other by electrostatic forces, forming an ionic compound.
For example, when sodium (a metal) reacts with chlorine (a non-metal), the sodium atoms lose one electron each to the chlorine atoms, forming sodium ions (Na+) and chloride ions (Cl-). These ions then combine to form sodium chloride (NaCl), an ionic compound.