It's a good thing Earth has a magnetic field! If it didn't, we'd be showered with solar radiation 24/7 and probably wouldn't survive for very long! But what is this magnetic field and how does it work? Astronomers have some idea…
The Earth's magnetic field is thought to be caused by a molten iron core deep inside Earth. With fancy math and physics one can prove that a molten electrically conductive material that is spinning will produce a magnetic field. This magnetic field is very similar to the one a bar magnet produces, just on a much larger scale. A bar magnet always has a north and south pole. If you sprinkle some iron filings around a bar magnet, you will see that they align themselves in a curved pattern extending from the north to the south pole. The iron filings are tracing the magnetic field lines, or in other words the force, produced by the magnet. One would naturally think that the north pole on Earth should represent the north end of the "bar magnet" inside Earth, and the south pole the south end, but it's actually the exact opposite! Let's stop and think… opposite ends of a magnet attract, while similar ends repel. So if a compass always points North, it must be attracted to the southern magnetic pole! Another fun fact about our magnetic poles is that they do not coincide with the physical north and south poles on Earth, they actually move quite a bit each year! In 2005, the north pole was measured to be at about 83N 114W, but today it is further west by roughly 10 degrees! Astronomers and geologists don't fully understand the production of Earth's magnetic field, and therefore don't fully understand why it is moving, especially at such a quick pace. There's much more work to be done on this subject.