The outer core of the Earth contains a lot of iron in a molten state. As the Earth spins this molten iron moves and creates the Earth's magnetic field. The Earth behaves as if there is a huge bar magnet at its centre.
A compass needle is a very small, thin magnet. Today, the north pole of a compass needle points towards the 'top' of the Earth. This means that the top of the Earth must be acting as a South magnetic pole (unlike poles attract).
Changing temperatures and the liquid iron flowing around the core cause the strength of the magnetic field to change but also cause the poles to switch places. Today, the top of the Earth is a South pole but in the past it has been a North pole.
Scientists have shown that there have been almost 200 pole switches in the last 100 million years. The last one was about 800,000 years ago.