Power ratchet wrenches, whether they be pneumatic or cordless, work in very much the same way. They typically have a motor that runs at a high RPM and in one direction only, regardless of whether a nut is being tightened or loosened. The output of the motor goes through planetary gearing, which reduces the speed and increases the torque, typically by a gear ratio of between 4:1 and 6:1. The output of the gearing drives a crankshaft with an offset pin on the end of it. This offset pin swings a yoke that is constrained to only move from side to side. With this arrangement, one complete turn of the crankshaft swings the yoke from one side to the other and back again. If you think of a manual ratchet, where you repeatedly swing the handle back and forth – the yoke is doing the same thing, just with a short angular stroke and at a much faster rate.
The inside diameter of the yoke is lined with gear teeth that surround and interact with a pawl-based ratcheting mechanism, much like in a manual ratchet. When the yoke swings from one side to the other, the teeth lock with the pawl to turn the tool output. Then when the yoke swings back, the teeth slip past the pawl without any movement of the tool output. The forward/reverse selector on the tool is used to change the orientation of the pawl so that it either locks with the yoke teeth for clockwise rotation of the tool output or for counter-clockwise rotation.