The US Geological Survey has a nice explanation of aftershocks ... and foreshocks. Key takeaway—earthquakes really shouldn't be thought of as a single event. Instead, "an" earthquake is really a swarm of earthquakes.
Aftershocks usually occur geographically near the main shock. The stress on the main shock's fault changes drastically during the main shock and that fault produces most of the aftershocks. Sometimes the change in stress caused by the main shock is great enough to trigger aftershocks on other, nearby faults, and for a very large main shock sometimes even farther away. As a rule of thumb, we call earthquakes aftershocks if they are at a distance from the main shock's fault no greater than the length of that fault. The automatic system keeps track of where aftershocks have occurred, and when enough aftershocks have been recorded to pinpoint the more and less active locations, the system adjusts the probabilities on the map to reflect those local variations.
Maggie Koerth-Baker is the science editor at BoingBoing.net. She writes a monthly column for The New York Times Magazine and is the author of Before the Lights Go Out, a book about electricity, infrastructure, and the future of energy. You can find Maggie on Twitter and Facebook.