This is because ionic compounds are electrically neutral. The positive charges from the cations must balance out the negative charges from the anions.
Here's a simple example:
* Sodium chloride (NaCl)
* Sodium (Na) has a +1 charge.
* Chlorine (Cl) has a -1 charge.
* The formula NaCl represents one sodium ion and one chloride ion, ensuring a net charge of zero: (+1) + (-1) = 0.