The formulas for chemical compounds have an overall charge of zero. This sum of zero results from adding up all the positive and negative charges in the formula.
Suppose atom A has a charge or oxidation number sometimes called valence of plus 3
and A combines with atom B with a charge of minus one.
The only way for the compound consistling of atoms of A and B to come out with an overall charge of zero is to combine one atom of A with three atoms of B. AB3
Lets say atom C has a charge of plus 2 and atom D a charge of minus 3
You can come out with a total charge of zero by combining three atoms of C with a total charge of plus 6 with two atoms of D with a total charge of minus 6
So we have put together formulas based on oxidation numbers of combining atoms.
Based on valence electrons.
If an atom has 7 valence electrons, it will gain one more electron to make a stable octet of 8 electrons. Thus its charge will be minus one We will call this an atom of Fluorine
Another atom has three valence electrons and by losing all three in a chemical reaction it will leave an octet of electrons exposed which was just below the outer energy level. Lets say this was an atom of Aluminum. It now has a charge of plus 3 because it lost three negative electrons.
When plus 3 Al combines with minus one charged F, the result is AlF3 With only two valence electrons, Ca will lose two electrons, leaving a stable octet below and take on a charge of plus 2. Phosphorous has 5 valence electrons, only 3 electrons away from an octet if it could gain three more electrons. By gaining 3 electrons from Ca, phosphorous atoms would have a charge of minus 3
So how would plus 2 Ca go with a minus 3 P. Total charge must come out zero, so with three Ca and two P the overall charge is zero, formula Ca3P2
Any atom with one valence electron will lose it taking on a charge of plus 1.
Three valence electrons on an atom are usually donated leaving an octet below and a plus 3 charge on the resulting ion. Six valence electrons, close to an octet by gaining 2 electrons, the atom will take on a charge of minus 2. This would be like Al2S3 Any atom with 6 valence electrons, (close to an octet) will usually gain 2 electrons taking on a charge of minus 2. This would be something like Na2S