The atomic number, #Z#, of #H#, #C#, and #O#, are #1#, #6#, and #8# respectively. These atomic numbers necessitate that there are #1#, #6#, and #8# protons (positively charged nuclear particles) in the respective nuclei.
Thus for each hydrogen atom, there must be 1 electron for neutrality, and it gets one of the two electron that comprise the #O-H# or #C-H# bonds (yes, a covalent bond is comprised of 2 electrons, one is owned by the hydrogen, and one is owned by #C#). So hydrogen is neutral.
For #C#, #Z##=# #6#. There are 2 inner core electrons, so there must be 4 electrons from the covalent bonds; carbon here has 4 covalent bonds, so each carbon possesses (or owns) 4 bonding electrons. Since each carbon thus has 6 electrons, the carbons are also neutral.
For #O#, #Z##=# #8#. There are 2 inner core electrons; it gets 1 electron each from its 2 covalent bonds, and there are 2 lone pairs of electrons (these lone pairs devolve solely to oxygen). Thus there are 8 electrons in total, that precisely balance the charge of the 8 nuclear protons.
You can do this with any molecule and assign formal charge. Covalent bonds are shared equally (i.e. each atom "owns" 1 electron); lone pairs and radicals devolve solely to the atom of interest.
Can you use this treatment to designate formal charge in methyl radical, #H_3C*#; #Ca^(2+)C-=C^(2-)#, calcium carbide; methyl anion #H_3C^-#, and sodium amide #H_2N^(-)Na^+#? Where does the formal charge reside?