Hi Kelli,
It is not well understood, however, anytime you have charged particles (for example, protons or electrons) that move, a magnetic field is created. The Sun is composed of mostly hydrogen, which in its neutral state has one proton and one electron. At the high temperatures in the solar interior (and some parts of the solar atmosphere), the electron is ripped away from the proton...hence a negatively charged particle is free to move...a magnetic field is generated. Now the details of why the Sun has an eleven-year sunspot cycle are not known, but this cycle has been observed to occur over the 400 or so years that we have been observing sunspots with a telescope (since Galileo). There may even be a 100-year cycle and only time will tell.
To learn more see:
http://solarscience.msfc.nasa.gov/the_key.shtml
http://solarscience.msfc.nasa.gov/dynamo.shtml
http://solarscience.msfc....v/SunspotCycle.shtml
http://solarscience.msfc....a.gov/interior.shtml