NOTE: The following example assumes output in lb-ft & horsepower. Dynamometers can also measure torque output in Newton Meters or Kilowatts just as easily, or any other measure of torque and power, for that matter.
First off, let's get everyone on the same sheet of music. When it comes to vehicles, there are two basic types of dynamometers: engine & chassis.
An engine dynamometer (dyno for short) measures the torque output from the crankshaft of an engine directly. Here is a larger engine dyno with an engine attached to it:
The engine attached to it appears to be a Detroit Diesel engine. Note that the engine is only attached to the dyno via an output shaft (covered by the yellow piece).
A chassis dyno measures the torque output of an engine as seen at the tires. Here is one which is above ground and with a car on top of it:
In the picture you can see a large cylinder underneath the rear (drive) wheels of the vehicle. Attached to the large cylinder is the measuring device for this particular model.
A dyno is designed to measure the torque at a given point (engine crank or at the wheels, depending on the model) and the rotational speed at which the torque is measured. To do this (let's say for an engine dyno), the engine is mounted (or as some would say "strapped") to a cradle. This cradle is in proximity to the dyno where the operator can place a connecting piece between the two. Then all of the electrical, fuel, and cooling systems are connected to the engine. Along with this, whatever sensors are present will be connected so that the operator can view the engine to ensure it is running properly or shut it down if they see issues. From there, the engine is ran and the dyno reads the amount of torque produced by the engine.
To measure the torque output of an engine, the dyno must create some kind of resistance, then measure the resistance. This resistance is then fed through a computer which computes the amount of torque at a given speed and from this, can compute the amount of horsepower. There are two main ways resistance can be applied against the engine.
A fluid type dyno uses a device much like the torque converter of an automatic transmission. The difference here is, the coupling device's resistance can be adjusted to control the speed of the engine.
Another type of dyno is an eddy current dyno. Instead of a fluid coupler, eddy currents are used to control the speed of the engine. Think of it as a giant generator which can apply load through creating the current, which stalls the speed of the engine.
Both fluid and eddy current dynos are called brake dynos because they use either method to produce a braking action which controls the engine. A completely different method to measure torque through a dyno is an inertial dyno which calculates the speed of how fast the engine or tires can speed up a known mass. This works off a completely different premise than a brake dyno. Due to this, measurements can be different between the two types.
As the engine is running on the dyno it develops torque. There are sensors attached to the dyno which can detect the amount of movement (actual twisting of the device itself) which is produced from the coupling device. This force is then computed into the amount of torque produced. During the testing, the engine is pushed into wide open throttle (WOT). The dyno produces resistance against the engine as it climbs through the rpm range. To measure the amount of torque, the resistance must be enough to hold the engine at a given speed, yet not overpower the engine (impeded it's progress through the rpm range). As the engine climbs in rpm, the sensor do their thing and read the amount of torque which is being produced.
A chassis dyno works pretty much the same way (in either fluid or eddy current), but is measured at the wheels (tires) as they contact the surface of the rotating drum. Resistance is put against the tires and the torque is measured. When measured at the tires, torque/horsepower output is always less than what would be measured at the crankshaft due to powertrain losses. Powertrain losses are those which are incurred as the power is transmitted through the transmission, drive line (if so equipped), changes of direction through the differential, out the axles, and through the tires. A rule of thumb dictates about a 15% loss when a vehicle uses a manual transmission and a loss of 18-20% when using an automatic transmission.
The calculation of horsepower (HP) is the easy part, mainly because it is just a mathematical equation which gives us the figure. To calculate HP, just follow the math:
P = (T * N) / constant
P = Power (hp)
T = Torque (lb-ft)
N = Rotational Speed (rpm)
C = Constant (5252)
NOTE: The constant 5252 is the rounded value of (33,000 ft·lbf/min)/(2π rad/rev)
Since this is purely an exercise in mathematics, the computer can figure out on the fly the exact amount of HP produced as long as it knows how fast the engine is going and the amount of torque produced at that given speed.