The Earth has several poles, not just two. It has geographic north and south poles, which are the points that mark the Earth's axis of rotation. It also has magnetic north and south poles, based on the planet's magnetic field. When you use a compass, it points to the magnetic north pole, not the geographic North Pole.
The Earth's magnetic poles move. The magnetic North Pole moves in loops of up to 50 miles (80 km) per day. But its actual location, an average of all these loops, is also moving at around 25 miles a year [ref]. In the last 150 years, the pole has wandered a total of about 685 miles (1102 kilometers). The magnetic South Pole moves in a similar fashion.
The poles can also switch places. Scientists can study when this has happened by examining rocks on the ocean floor that retain traces of the field, similar to a recording on a magnetic tape. The last time the poles switched was 780,000 years ago, and it's happened about 400 times in 330 million years. Each reversal takes a thousand years or so to complete, and it takes longer for the shift to take effect at the equator than at the poles. The field has weakened about 10% in the last 150 years. Some scientists think this is a sign of a flip in progress.
The Earth's physical structure is behind all this magnetic shifting. The planet's inner core is made of solid iron. Surrounding the inner core is a molten outer core. The next layer out, the mantle, is solid but malleable, like plastic. Finally, the layer we see every day is called the crust.
Image courtesy USGS
The Earth itself spins on its axis. The inner core spins as well, and it spins at a different rate than the outer core. This creates a dynamo effect, or convections and currents within the core. This is what creates the Earth's magnetic field -- it's like a giant electromagnet.
Exactly how the dynamo effect changes the field isn't widely understood. Shifts in the core's rate of spin and the currents within the molten material most likely affect the planet's field and the location of the poles. In other words, the poles move because the convection in the core changes. These changes might also cause the poles to switch places. Irregularities where the core and mantle meet and changes to the Earth's crust, like large earthquakes, can also change the magnetic field.