Question: how do you know how many electrons an atom gains or loses?
Answer: Just determine whether it is easier to lose the electrons to become stable or gain them. If they have less than three valence electrons, it is easier to lose them and become a positively charged ion. If they have more than four valence electrons, it is easier to gain electrons and become negatively charged.