Before we can understand how Earth's magnetic field works, we need to first have a basic understanding of magnetism. Magnetic fields are formed when electric charges move through magnetic materials like iron.
Any magnetised material is bipolar, which means it has a north and south pole, and the magnetic field lines run from north to south. The magnetic field lines at the north pole swing back round to the south pole, creating an external magnetic field outside the material that can influence other things that get too close.
You're probably familiar with a bar magnet, and in essence, Earth's magnetic field is very similar to that; imagine a giant bar magnet running through the core of Earth from pole to pole and you'll get the picture. However, Earth's core is molten, so our planet's magnetic field is induced by a circulating electric current at the core. One of the outcomes of this is that, on rare occasions, Earth's magnetic field can flip. This is believed to happen once every 200,000 years on average.
Taking the 'bar magnet' through Earth analogy further, it just so happens that the south pole of Earth's magnetism is at the geographic north pole, and the north pole is at the geographic south pole. When someone refers to 'magnetic north', they're actually referring to the south pole of Earth.
Earth's magnetic field is also not perfectly aligned with the rotation of the planet, instead of being tilted at an angle of 11 degrees. It's also not stationary; the magnetic poles are constantly moving, and indeed the south magnetic pole (at geographic north) has drifted up to 1,100 kilometres (684 miles) across the Canadian Arctic over the past four centuries.
Interestingly, though, despite the size of Earth, the magnetic field is weaker than a fridge magnet. However, that's still enough to protect us from harmful radiation from the Sun and elsewhere in the galaxy and helps our planet retain its atmosphere.
Origin of the Magnetic field
As already mentioned, Earth's magnetic field is the result of the moving electric field in the liquid molten iron core. Compared to the surface, the magnetic field at the core is about 50 times stronger.
It's likely that Earth has had a magnetic field for pretty much the entirety of its 4.5 billion-year lifetime. However, when Earth first formed, it's likely that the entire core was liquid; at the moment, only the outer core is liquid, with the inner core being solid due to the intense pressure. This means that Earth's early magnetism was likely stronger than it is now. Exactly how much stronger we can't be sure, but it's believed this strong magnetic field helped Earth retain an atmosphere early in its life, in the opposite way that Mars has lost its atmosphere as its magnetic field has dissipated.
Future of the magnetic field
Earth's magnetic field is weakening, but the exact reason why is poorly understood. However, this is no cause for concern; records suggest it decreases and increases in intensity relatively frequently. Since German mathematician Carl Friedrich Gauss first measured its strength in 1845, it has dropped about ten per cent.
If the magnetic field drops significantly further, there is a chance the magnetic field could flip. Contrary to popular belief, however, this will not signify the end of the world. The magnetic field has been known to flip many times over the last billion years, and life has survived. Therefore it's unlikely another flip would cause any devastating effects.
The only true danger is if the magnetic field were to disappear completely. As long as Earth has a liquid core, though, it will continue to have a magnetic field. Unless you're still around in a few billion years when such an event could occur, you haven't got much to worry about.