The process takes place at a biological membrane. In prokaryotes this is the plasma membrane, and in eukaryotes it is the inner of the two mitochondrial membranes. NADH and FADH2, electron carrier molecules that were "loaded" during the citric acid cycle, are used in an intricate mechanism (involving NADH-Q reductase, cytochrome c oxidase, and cytochrome reductase) to pump H+ across the membrane against a proton gradient.
A large protein complex called ATP synthase is embedded in that membrane and enables protons to pass through in both directions; it generates ATP when the proton moves with (down) the gradient, and it costs ATP to pump a proton against (up) the gradient. Because protons have already been pumped into the intermembrane space against the gradient, they now can flow back into the mitochondrial matrix via the ATP synthase, generating ATP in the process. The reaction is: