By way of example, oxygen has atomic number (Z) =8. The oxygen atom has 8 protons in its nucleus (this is what makes it an oxygen atom). The neutral atom therefore should have 8 electrons, but 2 of these are conceived to be inner core, and most of the time we depict oxygen in a molecule as R-O-R, with 2 lone pairs on the oxygen. These 2 lone pairs have a charge that is deemed to be associated entirely with the O atom; meanwhile the O centre is conceived to have half a share of the 2 electrons that form the C-O bond, i.e. 1 electron. So, for the assignment of formal charge, the oxygen atom has 8 protons in its nucleus, 8 positive charges, balanced by 2 inner shell electrons, plus the 2 electrons from the covalent bonds (this is 1 electron from each O-C bond, plus the 4 electrons from the 2 lone pairs, whose charge devolves to the O atom solely.
Now let's take this example further. Oxygen in alcohol, R-O-H, "owns" 6 valence electrons, and is neutral. Oxygen in alkoxide (or hydroxide), "owns" 7 valence electrons, and is therefore formally represented as R-O^-, with the negative charge localized on the oxygen atom (3 lone pairs + 1 electron from the C-O bond). The oxide dianion, O^(2-), that occurs in many salts but is unstable in water (why?), has formally 8 valence electrons. If I have been unclear or hard to follow please state your objections, and I'll have another go.