In an ionic compound the total number of charges on the cations must equal the total number of charges on the anions; that is, the compound must be neutral. In Section 3.2 we described how a sodium atom can donate an electron to another atom in order to form an ion with a full octet of electrons in its outermost electron shell (the same electron configuration as Ne). The charge on the sodium atom, when this happens, is now 1+, because it has eleven protons in its nucleus, but is only surrounded by 10 electrons. Lithium, likewise, can lose one electron to form Li1+ and be left with the same electron configuration as He. In fact, all of the Group 1A metals can lose a single electron to form 1+ ions. Elements in Group 2A can each lose two electrons to form 2+ ions and achieve a noble gas configuration. In fact, the group that a main-group element is associated with in the periodic table will dictate the valence (or charge) of its corresponding ion. Metals in Groups 1A, 2A and 3A will form ions with 1+, 2+ and 3+ charges, respectively.
Main-group nonmetals can easily achieve an octet of valence electrons by accepting electrons from other elements. Thus Group 5A elements can accept three electrons to form 3- ions, Group 6A elements accept two electrons to form 2- ions and Group 7A elements (the halogens) accept one electron to form 1- ions. For example, oxygen (Group 6A) needs to accept two electrons to achieve the electron configuration of neon. This gives oxygen a total of 10 electrons, but it only has eight protons in its nucleus (its atomic number is 8), therefore, the oxygen ion has a net charge of 2- (O2-).
To write a formula for an ionic compound composed of main group elements (or containing polyatomic ions) you need to adjust the ratio of anions and cations so that the resulting molecule is electrically neutral. For example, consider an ionic compound containing sodium and chlorine. Lithium is a Group 1A element and will form a 1+ ion; fluorine is a Group 7A element and will form a 1- ion. Neutrality is achieved when one lithium is paired with one fluorine, or LiF. For a compound composed of calcium and chlorine, the Group 2A calcium will form a 2+ ion while chlorine forms a 1- ion. To achieve neutrality, there must be two chlorines for every calcium, and the formula must be as CaCl2. Aluminum (Group 3A) will form a 3+ ion. If this was paired with oxygen (Group 6A) which forms a 2- ion neutrality would only be achieved if two Al3+ ions (for a total of six positive charges) were paired with three O2- ions (a total of six negative charges).
Consider a compound consisting of sodium and the polyatomic ion sulfate (SO42-). Sodium (Group 1A) yields a 1+ cation and so there must be two sodiums in the compound for every sulfate (which has a 2- charge), or Na2SO4. For a compound containing calcium (Group 2A) and nitrate (NO3-), two nitrate anions must be present for every calcium 2+ cation. In a compound containing multiple copies of a polyatomic ion, the entire ion is enclosed in parenthesis with a subscript to indicate the number of units. Thus the compound from calcium and nitrate would be written as Ca(NO3)2.