Gary A. Glatzmaier of the Institute of Geophysics & Planetary Physics at Los Alamos National Laboratory has done extensive work in this area. He replies:
"The Earth's magnetic field is thought to be generated by fluid motions in the liquid, outer part of the Earth's core, which is mainly composed of iron. The fluid motions are driven by buoyancy forces that develop at the base of the outer core as the Earth slowly cools and iron condenses onto the solid, inner solid core below. The rotation of the Earth causes the buoyant fluid to rise in curved trajectories, which generate new magnetic field by twisting and shearing the existing magnetic field. Over 99 percent of the Earth's magnetic energy remains confined entirely within the core. We only observe the small portion of the magnetic field that extends to the surface and beyond, where its basic structure is a dipole--that is, a simple north-south field like that of a simple bar magnet. There are also smaller, non-dipolar structures in the Earth's field; these change locally and very slightly on a century timescale.
"The dipole part of the field is usually aligned fairly closely with the Earth's rotation axis; in other words, the magnetic poles are usually fairly close to the geographic poles, which is why a compass works. Occasionally, however, the dipole part of the field reverses, causing the locations of the north and south magnetic poles to switch. This reversal process can be seen in the paleomagnetic record, locked into rocks of the ocean floor and in some lava flows. The reversal process is not literally 'periodic' as it is on the sun, whose magnetic field reverses every 11 years. The time between magnetic reversals on the Earth is sometimes as short as 10,000 years and sometimes as long as 25 million years; the time it takes to reverse is only about 5,000 years.
"The first dynamically-consistent, three-dimensional computer simulation of the geodynamo (the mechanism in the Earth's fluid outer core that generates and maintains the geomagnetic field) was accomplished and published by Paul H. Roberts of the University of California at Los Angeles and myself in 1995. We programmed supercomputers to solve the large set of nonlinear equations that describe the physics of the fluid motions and magnetic field generation in the Earth's core. The simulated geomagnetic field, which now spans the equivalent of over 300,000 years, has an intensity, a dipole-dominated structure and a westward drift at the surface that are all similar to the Earth's real field. Our model predicted that the solid inner core, being magnetically coupled to the eastward fluid flow above it, should rotate slightly faster than the surface of the Earth. This prediction was recently supported by studies of seismic waves passing through the core.
"In addition, the computer model has produced three spontaneous reversals of the geomagnetic field during the 300,000-year simulation. So now, for the first time, we have three-dimensional, time-dependent simulated information about how magnetic reversals can occur. The process is not simple, even in our computer model. Fluid motions try to reverse the field on a few thousand-year timescale, but the solid, inner core tries to prevent reversals because the field cannot change (diffuse) within the inner core nearly as quickly as in the fluid, outer core. Only on rare occasions do the thermodynamics, the fluid motions and the magnetic field all evolve in a compatible manner that allows for the original field to diffuse completely out of the inner core so the new dipole polarity can diffuse in and establish a reversed magnetic field. The stochastic (random) nature of the process probably explains why the time between reversals on the Earth varies so much."
For more detailed explanations of the geodynamo, the simulated magnetic reversals and the super-rotation of the Earth's inner core, Glatzmaier recommends the following papers: