How is ATP generated by cellular respiration?
One note at the beginning: please do not be scared! You can scroll right down to the bottom of this answer to read the conclusion which in itself should sufficiently answer the question. The bits in between are merely elaboration and need not be known in full detail.



The reaction one typically receives in high school biology classes to explain cellular respiration is already a simplification on many levels. It is presented as $(0)$, but in reality that is not one single reaction. Whether or not $\ce{ATP}$ has already been put in is irrelevant.

$$\ce{C6H12O6 + 6 O2 -&gt; 6 CO2 + 6 H2O}\tag{0}$$

In fact, this reaction is a series of steps, each performed by a single, specific enzyme. The first reaction, that basically immediately happens when glucose enters an animal cell is phosphorylation as shown in $(1)$.

$$\ce{\underset{{glucose}}{C6H12O6} + \underset{{ATP}}{C10H12N5O13P3^4-} -&gt;[enzyme] \underset{{glucose-6-phosphate}}{C6H11O9P^2-} + \underset{{ADP}}{C10H12N5O10P2^3-} + H+}\tag{1}$$

I have written $\ce{ATP}$ and will continue to do so for all products in the form they are in at physiological $\mathrm{pH}$

Glucose-6-phosphate is isomerised to fructose-6-phosphate, which cannot be distinguished by sum formulae alone, so I’m leaving this step out. Another enzyme comes along which phosphorylates fructose-6-phosphate again to give fructose-1,6-bisphosphate; see $(2)$. So far, all we have done is use up $\ce{ATP}$ at no benefit for the cell.

$$\begin{multline}\ce{\underset{{fructose-6-phosphate}}{C6H11O9P^2-} + \underset{{ATP}}{C10H12N5O13P3^4- -&gt;[enzyme] }} \\ \ce{\underset{{fructose-1,6-bisphosphate}}{C6H10O12P2^4-} + \underset{{ADP}}{C10H12N5O10P2^3-} + H+}\end{multline}\tag{2}$$

In a retro-aldol reaction, fructose-1,6-bisphosphate is split into glycerinaldehyde-3-phosphate (commonly abbreviated GAP) and dihydroxyacetonephosphate (commonly abbreviated DHAP). These once again are structural isomers that bear the same sum formula and they are interconverted. Hence I am pretending in equation $(3)$ that only GAP is a product. This assumption is close enough to valid in sum formulae.

$$\ce{\underset{{fructose-1,6-bisphosphate}}{C6H10O12P2^4-} -&gt;[enzyme] 2 \underset{{GAP}}{C3H6O5P^2-}}\tag{3}$$

We have now reached the phase of glycolysis (the consumption of glucose) called the pay-off phase. Previously, as you may have noticed, all we did was consume ATP. It is now almost time to get some back. But first, we need to prepare GAP for its prominent role in reaction $(4)$:

$$\begin{multline}\ce{2 \underset{{GAP}}{C3H5O6P^2-} + 2 HPO4^2- + 2 \underset{NAD+}{C21H26N7O14P2-}} \\ \ce{-&gt;[enzyme] 2 \underset{{1,3-bisphosphoglycerate}}{C3H4O10P2^4-} + 2 \underset{NADH}{C21H27N7O14P2^2-} + 2 H+}\end{multline}\tag{4}$$

It may seem confusing that I labelled an anion as $\ce{NAD+}$ (its common name). This is because there are two phosphate diesters that can each be protonated once. Fully protonated, $\ce{NAD+}$ is indeed a cation — only not at physiological $\mathrm{pH}$.

In the previous step, we synthesised a phosphoric carboxylic mixed acid anhydride. These are very energetic and consequently its hydrolysis is now used in reaction $(5)$ to synthesise $\ce{ATP}$. We are breaking even.

$$\begin{multline}\ce{2 \underset{{1,3-bisphosphoglycerate}}{C3H4O10P2^4-} + 2 \underset{{ADP}}{C10H12N5O10P2^3-} -&gt;[enzyme]} \\ \ce{2 \underset{{3-phosphoglycerate}}{C3H4O7P^3-} + 2 \underset{{ATP}}{C10H12N5O13P3^4-}}\end{multline}\tag{5}$$

After this step, we again have an isomerisation that cannot be shown in sum formulae. The phosphate group is moved from position 3 to position 2 giving 2-phosphoglycerate. This can then be dehydrated as shown in $(6)$ to give phosphoenolpyruvate: a very important cellular intermediate. We have also generated our first two water molecules.

$$\ce{2 \underset{{2-phosphoglycerate}}{C3H4O7P^3-} -&gt;[enzyme] 2 \underset{{phosphoenolpyruvate}}{C3H2O6P^3-} + 2 H2O}\tag{6}$$

Finally, a second set of two $\ce{ATP}$ molecules is formed by dephosphorylating phosphoenolpyruvate to give free pyruvate as shown in reaction $(7)$.

$$\begin{multline}\ce{2 \underset{{phosphoenolpyruvate}}{C3H2O6P^3-} + 2 \underset{{ADP}}{C10H12N5O10P2^3-} + 2 H+} \\ \ce{-&gt;[enzyme] 2 \underset{{pyruvate}}{C3H3O3-} + 2 \underset{{ATP}}{C10H12N5O13P3^4-}}\end{multline}\tag{7}$$

We can draw an intermediate line here, since the process typically known as glycolysis has reached its close. We can sum it up in reaction $\text{(1–7)}$. We still have not generated any $\ce{CO2}$ and only $\ce{2 H2O}$. We also already have $\ce{2 ATP}$ per molecule of glucose and 2 mysterious molecules called $\ce{NADH}$.

$$\begin{multline}\ce{\underset{{glucose}}{C6H12O6} + 2 \underset{{ADP}}{C10H12N5O10P2^3-} + 2 HPO4- + 2 \underset{NAD+}{C21H26N7O14P2-} -&gt;[steps]} \\ \ce{2 \underset{{pyruvate}}{C3H3O3-} + 2 \underset{NADH}{C21H27N7O14P2^2-} + 2 H+ + 2 \underset{{ATP}}{C10H12N5O13P3^4-} + 2 H2O}\end{multline}\tag{1–7}$$

The next step is still very easy to explain. It is called oxidative decarboxylation and adds a cofactor known as coenzyme A of $\ce{CoA}$ covalently to what was pyruvate before the reaction. See equation $(8)$.

$$\begin{multline}\ce{2 \underset{{pyruvate}}{C3H3O3-} + 2 \underset{NAD+}{C21H26N7O14P2-} + 2 \underset{{coenzyme A}}{C21H33N7O16P3S^3-} -&gt;[enzyme]} \\ \ce{2 \underset{{acetyl-CoA}}{C23H35N7O17P3S^3-} + 2 \underset{NADH}{C21H27N7O14P2^2-} + 2 CO2}\end{multline}\tag{8}$$

Acetyl-CoA now enters into a magic known as the Krebs cycle or citric acid cycle. As the name implies, this is constantly going around. At the beginning, the two carbon atoms of acetyl-CoA are added to oxaloacetate. After the ten cycle steps, we will have lost two molecules of $\ce{CO2}$, effectively reforming the oxaloacetate substrate. And once again some of the steps are merely isomerisations which do not show up in some formulae. Are you ready? Here goes. Reaction $(9)$ has already been described in the text above:

$$\begin{multline}\ce{2 \underset{{acetyl-CoA}}{C23H35N7O17P3S^3-} + 2 \underset{{oxaloacetate}}{C4H2O5^2-} + 2 H2O} \\ \ce{-&gt;[enzyme] 2 \underset{{citrate}}{C6H5O7^3-} + 2 \underset{{coenzyme A}}{C21H33N7O16P3S^3-}}\end{multline}\tag{9}$$

After isomerisation of citrate to give isocitrate, this is oxidised to give oxalosuccinate as shown in $(10)$.

$$\begin{multline}\ce{2 \underset{{isocitrate}}{C6H5O7^3-} + 2 \underset{NAD+}{C21H26N7O14P2-} -&gt;[enzyme]} \\ \ce{2 \underset{{oxalosuccinate}}{C6H3O7^3-} + 2 \underset{NADH}{C21H27N7O14P2^2-} + 2 H+}\end{multline}\tag{10}$$

For each oxalosuccinate, one molecule of $\ce{CO2}$ can be removed to give &alpha;-ketoglutarate as shown in $(11)$.

$$\ce{2 \underset{{oxalosuccinate}}{C6H3O7^3-} + 2 H+ -&gt;[enzyme] 2 \underset{\alpha{-ketoglutarate}}{C5H4O5^2-} + 2 CO2}\tag{11}$$

&alpha;-ketoglutarate is then once again attacked by coenzyme A in the same manner as in step $(9)$. This can happen because both pyruvate and &alpha;-ketoglutarate have the same structural feature. Refer to equation $(12)$.

$$\begin{multline}\ce{2 \underset{\alpha{-ketoglutarate}}{C5H4O5^2-} + 2 \underset{NAD+}{C21H26N7O14P2-} + 2 \underset{{coenzyme A}}{C21H33N7O16P3S^3-} -&gt;[enzyme]} \\ \ce{2 \underset{{succinyl-CoA}}{C25H36N7O19P3S^4-} + 2 \underset{NADH}{C21H27N7O14P2^2-} + 2 CO2}\end{multline}\tag{12}$$

We have now lost all $\ce{CO2}$. What we need to do is regenerate the oxaloacetate from the beginning of the Krebs cycle. The first step of this is to generate succinate from succinate-CoA by hydrolysis as given in $(13)$.

$$\begin{multline}\ce{2 \underset{{succinyl-CoA}}{C25H36N7O19P3S^4-} + 2 H2O + 2 \underset{{ADP}}{C10H12N5O10P2^3-} + 2 HPO4^2-  -&gt;[enzyme]} \\ \ce{2 \underset{{succinate}}{C4H4O4^2-} + 2 \underset{{coenzyme A}}{C21H33N7O16P3S^3-} + 2 \underset{{ATP}}{C10H12N5O13P3^4-}}\end{multline}\tag{13}$$

Next, we oxidise the saturated succinate molecule to an unsaturated fumarate. This is typically perfomed by yet another cofactor called flavin-adenine-dinucleotide or $\ce{FAD}$; see reaction $(14)$.

$$\ce{2 \underset{{succinate}}{C4H4O4^2-} + 2 \underset{FAD}{C27H31N9O15P2^2-} -&gt;[enzyme] 2 \underset{{fumarate}}{C4H2O4^2-} + 2 \underset{FADH2}{C27H33N9O15P2^2-}}\tag{14}$$

Water is added to the double bond to give malate (reaction $(15)$):

$$\ce{2 \underset{{fumarate}}{C4H2O4^2-} + 2 H2O -&gt;[enzyme] 2 \underset{{malate}}{C4H4O5^2-}}\tag{15}$$

And malate is finally oxidised by using $\ce{NAD+}$ as shown in $(16)$ to give the starting material of the Krebs cycle, oxaloacetate.

$$\begin{multline}\ce{2 \underset{{malate}}{C4H4O5^2-} + 2 \underset{NAD+}{C21H26N7O14P2-} -&gt;[enzyme]} \\ \ce{2 \underset{{oxaloacetate}}{C4H2O5^2-} + 2 \underset{NADH}{C21H27N7O14P2^2-} + 2 H+}\end{multline}\tag{16}$$

Technically, we could end it here. Let’s look again at the final products of reactions $(1–16)$. Thankfully, a lot cancels out.

$$\begin{multline}\ce{\underset{{glucose}}{C6H12O6} + 4 \underset{{ADP}}{C10H12N5O10P2^3-} + 4 HPO4^2- + 10 \underset{NAD+}{C21H26N7O14P2-} +} \\ \ce{2 \underset{FAD}{C27H31N9O15P2^2-} -&gt;[steps] 6 CO2 + 6 H+ +} \\ \ce{4 \underset{{ATP}}{C10H12N5O13P3^4-} + 10 \underset{NADH}{C21H27N7O14P2^2-} + 2 \underset{FADH2}{C27H33N9O15P2^2-}}\end{multline}\tag{1–16}$$

We’re still not there yet. We have generated a lot of $\ce{NADH}$ and $\ce{FADH2}$ which somehow needs to be transformed back into $\ce{NAD+}$ and $\ce{FAD}$ for the next iteration to be performed — remember that cellular energy is, as you noted in your question, typically equated to $\ce{ATP}$. And we still haven’t used oxygen at all and are not producing enough water.

The next set of reactions to bring us closer to our goal is the electron transport chain. By a series of reactions that is not easily described (but basically involve electrons being moved in redox processes from one compound to another), oxygen molecules acquire four electrons and four protons each to become water. The electron transport chain that performs this will, at the same time, use the energy liberated by the generation of water to set up a proton potential: protons ($\ce{H+}$) get pumped from one side of an intracellular membrane to another.

This proton potential of course is a driving force since there is a lack of protons on the other side. Protons may travel back through special trans-membrane proteins. These proteins can be imagined like a watermill and start rotating as protons pass through. This rotation allows them to literally slap a phosphate onto an $\ce{ADP}$ molecule to give $\ce{ATP}$.

As a theoretical maximum, the regeneration of $\ce{NAD+}$ from $\ce{NADH}$ gives around 2.5 equivalents of $\ce{ATP}$ while $\ce{FADH2}$ can only be used to generate around 1.5 equivalents of $\ce{ATP}$ upon the reverse reaction to $\ce{FAD}$. These numbers are, however, generalised, since some energy is also lost in transport and other processes.

Therefore, our theoretical maximum yield of $\ce{ATP}$ is 25 from $\ce{NADH}$, a further 3 from $\ce{FADH2}$ and a further 4 that are directly produced in the reaction sequences. Each of these $\ce{ATP}$s is generated by the reaction of a phosphate anion onto an $\ce{ADP}$ molecule, technically involving the loss of water or an equivalent.



As you may see, writing up a complete reaction for the entire cellular respiration process is very difficult. We can, however, arrive approximately there by a few basic principles:


there are up to $\ce{32 ATP}$ generated over the course of the digestion of one molecule of glucose
each $\ce{ATP}$ is generally synthesised by some form of $\ce{HPO4- + ADP}$ — therefore, the phosphorus part is balanced.
the exact process is not necessarily important; a lot cancels out and the end result counts.


This in total may lead us to question why exactly the reaction taught at schools is often the one presented in equation $(0)$. Well, we can see that exactly six molecules of carbon dioxide are formed in the overall process per molecule of glucose. This makes sense, as it means each glucose carbon atom effectively leaves the body as $\ce{CO2}$, the carbon compound with the lowest remaining energy. The number of oxygen and water molecules cannot, unfortunately, be accurately established for the single process. However, schools want to teach the concept of a balanced equation. And if all carbon leaves the body as $\ce{CO2}$, then all hydrogen must eventually leave it as $\ce{H2O}$. To accomplish that, a certain set of oxygen molecules is needed — exactly 6.

The reaction $(0)$ thus never actually happens in our body. That is a good thing. You can see what it looks like by heating suger to very high temperatures and then adding a match — actually don’t. It’s the same combustion as any other combustion. Part of it can be adequately summarised of many partial reactions, part of it is just there so the summary fulfills the required stoichiometry. And even the $\ce{32 ATP}$ are only an approximation based on a lot of averaging.

What we can definitely say, however, is that each of the $\ce{32 ATP}$s is produced by the reaction of phosphate and $\ce{ADP}$. And therefore, at least consistent with your high-school level, the reaction should better be written as:

$$\begin{multline}\ce{\underset{{glucose}}{C6H12O6} + 6 O2 + 32 HPO4- + 32 \underset{ADP}{C10H12N5O10P2^3−}} \\ \ce{-&gt; 6 CO2 + 6 H2O + 32 \underset{ATP}{C10H12N5O13P3^4−}}\end{multline}\tag{full}$$