Sunspots and their behavior are direct manifestations of the Sun’s magnetic field, which we still don’t fully understand. Here’s what we do know: Ex-tremely hot gas, called plasma, in the Sun’s outer atmosphere rotates differentially — faster at the middle than at the poles — while plasma in the interior rotates as a solid body. The rotation of the charged solar plasma produces a current. This current creates a magnetic field like that of a bar magnet with a north pole and a south pole aligned with the axis of rotation, but the differences of rotation speed mean the magnetic field becomes wound up and tangled.
Astronomy magazine subscribers can read the full answer for free. Just make sure you're registered with the website.