Ionic compounds are the compounds that are formed by the gain or loss of electrons. In other words these are formed by the attraction of cations and anions.
Ex :
2Na + Cl2 →2Na+ + 2Cl− → 2NaCl
Sodium is a group 1 metal, thus forms a +1 charged cation. Chlorine is a non-metal, and has the ability to form a -1 charged anion. These two together form sodium chloride.