To balance the charges of ions the number of electrons lost is equal to the number of electrons gained. The overall net charge must be zero.
The number of ions needed to accomplish this is represented by the subscripts.
For any given ionic compound, the product of the charge on the ion (or multiatomic ionic group) times the subscript of the ion will give a total charge of zero when all ions are considered.