The Earth has a magnetic field, in many ways similar to a bar magnet.
Hopefully you played with a bar magnet in school. You sprinkle iron filings on a piece of paper and put the magnet underneath and the iron shavings rearrange themselves into a lovely set of curves, converging at the magnetic poles and spreading out more halfway between them. The overall shape is like an apple cut in half.
The Earth has a magnetic north pole and a magnetic south pole (not to be confused with the geographic, or rotational, poles) just like that bar magnet — we call this a dipole field. But the mechanism creating the magnetic fields between the two are way different.
In a bar magnet, it's due to iron atoms; each has a small magnetic field, and the atoms themselves are aligned in such a way that they all add together, creating the overall magnetic field.
In the Earth it's far more complicated. The Earth's core is composed of two layers; the inner core, which is solid, and the outer core, which is liquid. The core is very hot, and the inner core heats the outer core above it. Heated from below, the liquid in the outer core convects: Hot fluid rises and cooler stuff sinks. But there's iron in the core and it's hot enough to be ionized, to have one or more electrons stripped off, giving atoms a net positive charge. When a charged particle moves it creates a magnetic field, and all those atoms moving in the same direction generate Earth's overall magnetic field.
As long as the Earth's core is hot, that outer core will convect, and the Earth's magnetic field will exist.
… but it's more complicated than that.
Over a century ago, scientists discovered that Earth's magnetic field sometimes switched polarity, where the north pole becomes the south pole and vice-versa. Mind you, this doesn't mean the Earth physically flips over — that's common refrain by doomsday conspiracy cultists, because they're either easily confused or want to confuse you — just that, say, if you use a compass it'll start pointing south instead of north.
It became clear over time that these reversals have happened many, many times. Almost 200 of them have been seen in the fossil record, going back over 80 million years! They're pretty rare on a human timescale, occurring about once every 100,000 to 1,000,000 years. The thing is, no one is really sure how long they take to go from start to finish. Partly this is because they happened a long time ago — the last one, called the Matuyama-Brunhes reversal, was nearly 800,000 years ago! — and also it's hard to get fine-resolution data, and because they seem to happen rapidly (in a geologic sense).
But a new study has been published looking at volcanic, sedimentary, and ice core records, and the scientist found that, in that most recent event, the whole thing took about 22,000 years, much longer than expected!
The Earth's magnetic field first started weakening about 795,000 years ago. After that it flipped polarity and strengthened, but before it could really settle down again it started to fluctuate and collapse again around 11,000 years later (784,000 years ago). The magnetic field fluctuated for some thousands of years after that, but then flipped polarity again and strengthened around 773,000 years ago, becoming the field we have today. That final flip may have only taken 4,000 years. So, instead of just a clean flip, it looked like it underwent three separate stages where sometimes it was dominated by a dipole field (though not really stable) and other times when it was more chaotic. The whole thing took more than twice as long as previous estimates, too.
Their methodology is a bit complicated. In volcanic rock, the Earth's magnetic field is recorded as lava cools; iron in the rock orients itself to the Earth's field, and so the geomagnetic field strength and direction can be measured. Radioactive isotopes can be used to date the timings of these events. For sedimentary layers, the amount of sunlight the Earth receives changes on timescales of a few tens of thousands of years due to changes in Earth's orbital shape (the Milankovitch cycles) and this can be seen in deposits of an isotope of oxygen.
My favorite one is from the ice cores. When the Earth's magnetic field is weaker, cosmic rays — super-energetic subatomic particles whizzing around in space at near the speed of light — can slam into our atmosphere. When they hit a nitrogen or oxygen atom nucleus they split the nucleus like a bullet going through a rock; the shrapnel from this includes an isotope of beryllium called 10Be. This then gets deposited in ice, and measured in ice cores, allowing scientists to trace the geomagnetic field that way.
So this is interesting! 22,000 years is a long time on a human scale, of course, but still rapid for a geological event. It's hard to say how representative that is for other reversals, but it's a place to start.
A big question still remains, and that's why this occurs. Hypotheses abound, but overall it's likely that the field gets tangled up inside the Earth after some triggering event, and when it resettles the polarity is reversed.
Another question is what happens during the reversal? Again, a lot of conspiracy theorists love to wax anti-scientific about it, but the truth is no one is really sure. It's likely not to have a huge affect on daily life, though there are bound to be some issues with it — for example, compasses using the Earth's magnetism won't work, but GPS or some equivalent can be used instead. We'll have to be careful though, since Earth's magnetic field protects us from things like the solar wind and cosmic rays, so satellites might be affected. Our thick atmosphere should do a pretty good job protecting us from everything else (like solar wind, cosmic rays, and so on).
One more thing. A lot of breathless headlines are generated when news like this comes around, usually in the "We're overdue for X" variety, whether it's an earthquake, a volcanic eruption, or a magnetic reversal, and generally implying we're all gonna die. The time since the last reversal is somewhat longer than average, but it's not like the planet sets a calendar for these things. It could start tomorrow, or it might not happen for another million years. So until geoscientists say, "Yup, here we go!" breathe easy.
And even then, breathe easy. Unlike movies (yikes, The Core) it won't cause massive catastrophes like bridges collapses and the sea boiling. And, in fact, it will tell us a lot more about what's going on deep inside the Earth, a place that otherwise is not all that easy to figure out.