The Sun's magnetic field lines get tangled over time as the Sun rotates. These tangled magnetic fields give rise to sunspots and solar activity such as solar flares and Coronal Mass Ejections (CMEs).
Credit: SOHO (ESA & NASA)
The Sun is not solid. It is a huge sphere of plasma, so it behaves like a gigantic ball of gas. Different parts of the Sun rotate at different rates. The Sun rotates more quickly at its equator than at its poles. This animation shows the different rate of rotation between the Sun's equator and its poles. At the start of the animation, magnetic field lines coming from the Sun are smooth and orderly. Over time, the Sun's differential rotation rates cause its magnetic field to become twisted and tangled.
The "tangles" in the magnetic field lines can produce very, very strong localized magnetic fields. Places where "ropes" of bundled field lines "break" the surface of the Sun are active regions where sunspots form. These active regions often generate solar "storms": solar flares and coronal mass ejections (CMEs).
When the Sun's magnetic field gets really twisted, there are lots of sunspots. Those times are the peaks of the 11-year sunspot cycle. There are lots of solar storms then. After a while, the Sun's "messy" magnetic field resets itself into a smoother, more orderly arrangement. Most sunspots go away. The cycle starts all over again. As the Sun spins and the smooth magnetic field gradually gets stretched, sunspots once again grow plentiful over the course of the next 11 or so years.
Each time the Sun resets with a new, smooth magnetic field, that field is reversed from the way it had just been. The Sun's North and South Magnetic Poles trade places. So it really takes about 22 years (two 11-year cycles) for the Sun's magnetic field to go through a complete cycle and end up back where it started.