You are probably familiar with magnets. Magnetized bits of iron can stick to metal things such as your refrigerator or paperclips. (It's always a fridge or paperclips, for some reason.) You may even have made an electromagnet by wrapping a wire many times around a nail (or some other iron thing) and running a current through that wire by attaching it to a battery.
When physicists want to describe the forces that result from magnetized objects like this, we talk about magnetic fields. Really, a magnetic field means the strength and direction of this abstract physical thing (called, er, the "magnetic field") in all regions of space around the spot we're talking about. We then know how to use the numbers associated with the strength and direction of this magnetic field to calculate how it will interact with other things, such as iron objects (that react to magnetic fields), other magnetic fields, and charged particles. We will often visualize these magnetic fields by drawing "magnetic field lines". These lines point along the direction of the magnetic field. Where magnetic field lines are closer together, it's a stronger magnetic field. Iron filings will tend to line themselves up along magnetic fields, allowing a pretty direct (and physical) visualization of small segments of magnetic field lines. The weakness of this visualization is that how "close together" the lines appear depends on where the iron filings fall as well as how strong the magnetic fields are.
Magnetic Fields... innnn... Spaaaaaace
There are magnetic fields all over the place in space. The Earth has a magnetic field associated with it. Indeed, the Earth's magnetic field is very similar to that of a huge bar magnet embedded in the Earth, with the south magnetic pole of the magnet pretty close to the North Pole of the Earth.
When a magnetic field is embedded in plasma— and plasmas are very common in space— the dynamics of the magnetic field and the dynamics of the plasma are closely linked. Charged particles interact with magnetic fields, and the motion of charged particles gives rise to magnetic fields. Wheres you can use simple [sic] fluid dynamics or hydrodynamics to describe the motion of a non-charged fluid (a liquid or a gas), when that fluid is made up of charged particles (and is therefore a plasma), you have to take into account the magnetic fields and start using what is called magnetohydrodynamics. The most important result of magnetohydrodynamics is that the magnetic field lines and the plasma will, for the most part, move together. If the plasma is dense enough, as it moves around, it will convect the magnetic field lines along with it. If the plasma is being compressed, or if it is twisting around, it can stretch out or squeeze magnetic field lines (which corresponds to a strengthening and weakening of the magnetic field), and it can also distort and twist up the magnetic fields. Just as there is energy in electric or gravitational fields, you can also store energy in magnetic fields, and as magnetic field lines get compressed and twisted up, a lot of energy can be stored in them.
Like the Earth, the Sun has a magnetic field. Unlike the Earth, the Sun is entirely gaseous, not solid. What's more, throughout much of the Sun the gas is ionized— that is, electrons are free, not trapped on atoms. That means that the particles in the gas are charged, and the gas is a plasma. While the physics of the Sun is by far dominated by the balance between its tremendous gravity and the tremendous pressure generated by the nuclear fusion at its core, magnetic fields are important, and give rise to things such as the Solar Corona (that wispy "crown" of extremely hot gas that you may have seen in pictures of Solar eclipses) and the various loops and prominences you see on the Sun, as well as solar flares and ejections of groups of charged particles from the surface of the Sun. The big loops you see sticking out on the Sun in some images such as the one below are where bundles of magnetic fields have blooped out of the surface of the Sun, and plasma has been carried along with them.
The Sun rotates, about once every 24 days or so. However, that rotation is not uniform; the poles rotate faster than the equator. This means that the magnetic fields in the Sun are always getting twisted up and distorted. Every so often the magnetic fields will "break", and relax to a less twisted state, releasing the energy that was stored in the magnetic fields. This is what causes so-called "coronal mass ejections" (CME). There is a constant stream of charged particles coming off of the Sun; when there's a CME, the sun briefly sends out charged particles at a much greater rate into the Solar System. If that mass ejection is pointed more or less in the direction of the Earth, then those particles will hit us.
Charged Particles Intersecting Magnetic Fields
Whereas an electric field points exactly along the direction that it will push a charged particle (or exactly opposite it, if the charge on that particle is negative, as is the case with an electron), when a charged particle comes across a magnetic field the force on it is perpendicular to both its direction of motion and to the magnetic field line. If you imagine that you're going straight, and then you feel a force to your left, you'll curve to the left. If you keep having a force to your left (your new left, now that you've curved), you'll eventually go around in circles. This is what happens when charged particles come into a region of magnetic fields. It depends on much energy the particles have— how fast they're moving— as well as the strength of the magnetic field. If they're moving very fast, they may just get deflected a little before going on their way. However, if the magnetic field is strong enough, and especially if the particle loses a little energy, say by colliding occasionally with other particles, it can get trapped by the magnetic field, at which point it will circle around it. In fact, particles will tend to spiral around magnetic field lines, moving along the lines as they orbit around them. That part of their velocity that is pointing along the magnetic field is not affected, but the part that's perpendicular circles around them.
Because the Sun is continually throwing charged particles at us, the Earth has an ample source of particles to collect in its magnetic fields. There are belts of radiation above the Earth, called the Van Allen Belts, where charged particles (mostly protons, or Hydrogen nuclei, and electrons) spiral around the Earth's magnetic field.
Look at the picture above of the Earth's magnetic field. Pick one of the field lines that is away from the surface of the Earth at the equator. If you follow that field line along, you'll see that as you get closer to the poles, eventually the field line plunges down into the Earth. If you trap charged particles on these field lines, as the charged particles move along them they will eventually intersect the atmosphere. As these charged particles plow into the atmosphere, they run into atoms in the atmosphere, giving them energy and exciting their electrons. When the atoms in the atmosphere release this energy, they glow. This is what we observe as the northern and southern aurorae. This also explains why you only tend to see the aurorae if you're fairly far north or fairly far south: you have to be there for the magnetic field lines that trap the charged particles to intersect the atmosphere. It also explains why you might expect more impressive aurorae after a big mass ejection from the Sun. When there are more charged particles coming off of the Sun, there's more basic material for the Earth's magnetic fields to capture and spiral down into the atmosphere.
Acceleration of Cosmic Rays
When a supernova explodes, it sends a blast wave out into the circumstellar blast. This blast wave is driven by the outer layers of the star that get blown off in the supernova, and the blast wave then sweeps up the existing circumstellar gas into its expansion. Remember above that we talked about magnetic field lines being locked into the fluid. If you have gas crashing into other gas in a shockwave with this expansion, you'll have gas getting highly compressed right at the shock. That also means that you're going to be squeezing together magnetic field lines right at the shock. As the magnetic field lines squeeze together, some of the kinetic energy from the expanding gas goes into accelerating the particles spiraling around those magnetic field lines. The result is that they spiral faster and faster around the stronger and stronger magnetic fields.
Eventually, with all those particles there, some particles will run into other particles and be knocked free. By this time, however, they will have picked up a lot of energy because of these compressing and strengthening magnetic fields, and the from the process of them spiraling faster and faster around those magnetic fields. As a result, by the time the particles are kicked out they might have quite a lot of kinetic energy. This fast-moving charged particles then go flying through the galaxy, and they're what we observe as cosmic rays here on Earth.
There is a constant stream of charged particles coming off of the Sun. Cosmic rays tend to have higher energy than that, though, and there are a lot of them that are too energetic to have come off of the Sun. Indeed, there are some cosmic rays that are too energetic, we believe, even to have come from supernovae, and their origin is still something of a mystery (or at least was last time I checked). However, the vast majority of the higher-energy cosmic rays that show up here at Earth were accelerated long ago through this mechanism, called the "Fermi mechanism", in the magnetic fields in the shocks of supernovae. Ever since then, these charged particles have been wandering about the Galaxy, steered by the magnetic fields of the galaxy, until by chance some of them run into our planet.