Figure 1 - Earth's internal structure: dense solid metallic core, viscous metalic outer core, mantle and silicate based crust.
In 1600, William Gilbert hypothesized that the Earth was a giant magnet. He wasn't far off! Deep within Earth exists a solid core composed mostly of iron about the size of the moon with temperatures on the order of 5,000K. The temperature differences between Earth's mantle and its much hotter solid core cause the fluid outer core material in between the core and the mantle to convect much like a pot of boiling water will stir because the surface of the water is cooler than the bottom of the pan. This movement of charged material combined with the Coriolis effect is thought to generate currents which in turn generate the Earth's magnetic field or "magnetosphere". Since the core, outer core, and mantle are not rigidly connected to eachother they do not have to spin at the same rate or in the same plane. In fact, Earth's magnetic field is tilted 11 degrees to its geometric axis of rotation. The specific solutions to why or how this happens are still being studied, but astronomers and planetary scientists see mis-alignments like this between rotation and magnetic axis in many different objects from planets to pulsars.
In the absence of other forces, Earth's magnetosphere would look very similar to the traditional bar magnet / iron filing experiment where iron filings line up along symmetric magnetic field lines. However, in the presence of the solar wind, the Earth's magnetosphere is compressed on the sunward side and hugely elongated on the anti-solar, or night side, producing a "geotail" that extends over 300,000 km (about the distance of the moon's orbit) away from Earth. A bow shock where solar wind and magnetic field strength just balance, extending over 60,000 km toward the sun represents the boundary between solar wind dominated space and magnetosphere dominated space. Within the magnetosphere cavity in the solar wind, ions and electrons (charged particles) rotate earth image around and are accelerated along magnetic field lines - giving them a sort of spiral trajectory. In addition, these charged particles gradually shift from field line to field line creating an electric current that encircles the Earth. Low-energy particles form the so-called the "ring current", while the high-energy particles form the van Allen Belts.
One consequence of this behavior is the generation of aurora (northern and southern lights). Aurora are caused by collisions between charged particles (protons and electrons) streaming down along magnetic field lines from the geotail region, and neutral gasses (primarily N2 and O2) in the Earth's atmosphere. These collisions add energy to the atmospheric gas molecules, which give off photons of light at specific frequencies in order to return to their ground energy states. O2 emits green or red light. N2 emits blue light. Aurora have been observed in the polar regions of all four gas giants as well.