Your heart is more like a car engine that you might realize. Loosely speaking, the heart's chambers are the pistons, the contraction of those chambers is the piston stroke, and the ignited gas is the blood that keeps everything going. The heart even has a sparkplug. An electrical impulse triggers each contraction and sets the timing of the whole process.
When someone needs a pacemaker, it's usually because there's a problem with these electrical impulses, which weakens the heartbeat, causing all sorts of issues. If the heart can't get enough blood pumping through the body, the body -- and especially the brain -- suffers from lack of oxygen. An artificial pacemaker sends out electrical impulses to mimic the heart's natural pacemaker, the sinoatrial node (SA node), located in the right atrium.
The SA node is a group of cells that generates electrical current. It sends out an electrical charge at some set interval -- say, once every second, which would establish the low-end normal heart rate of 60 beats per minute (60 to 80 is a healthy heart rate). These impulses are the "sparks" that cause the right atrium to contract, starting the whole string of events that gets blood pumping in waves through your body. It's this electrical impulse that sets the rhythm of your heart. Whenever the SA node sends out a charge, your heart beats. When you need more blood pumping, like when you need more oxygen to climb steps or run a mile, the SA node shortens its electrical-discharge interval.
There are actually two pacemakers. The SA node is the primary; the atrioventricular node (AV node), located in a bundle of tissues on the border between the right atrium and the right ventricle, is the secondary. When the SA node sends out an electrical impulse, the first place it goes is to the AV node. While the SA node sets the rhythm of your pulse, the AV node sets the rhythm of your heart contractions. It delays the signal on its way to the ventricle, giving the atrium time to contract first. It holds it up for about a tenth of a second [source: Signalife]. If the atrium and the ventricle contracted at the same time, the ventricles would push out their blood before they were totally full, resulting in low blood pressure, among other problems.
When the heart's electrical system misfires, it's called atrial fibrillation. Basically, what happens is the heart starts generating electrical impulses in more than one place, not just in the SA node. This messes everything up and can result in a pulse well above the 60 to 80 range that a healthy heart generates. With too many triggers, the right atrium can't possibly contract fully each time, meaning it never gets a full pump of blood into right ventricle, and the body gets deprived of blood. An artificial pacemaker stabilizes the system by taking over the job of sending out electrical impulses, getting the heart back into a regular rhythm.
For more information on the heart, atrial fibrillation and related topics, look over the links on the next page.