Each atom in an ionic compound tends either to empty or complete the shell to 8 electrons. So when the number of electrons on the external shell is near 8 , the atom tends to attract electrons to complete the shell giving a positive ion called anion
Conversely when the number of electrons is small the atom gives these electrons to form a positive ion called a cation