An ionic compound is formed by the transfer of one or more electrons from a metal to a nonmetal. The positive and negative ions that form are held together by electrostatic attraction. Ionic compounds don't exist as individual molecules. Instead they form a crystal lattice which is a network of repeating positive and negative ions. We think of sodium chloride, the most common ionic compound, as NaCl but even small specks of it contain many Na+ and Cl- ions in a highly organized crystal formation.
The ions that form from metals and non-metals do so because metals have a high tendency to lose electrons and non-metals have a high tendency to gain electrons. This exchange of electrons provides a stable electron configuration (complete octet) for both species.