We do not know for certain, but internal convection and the differential rotation of the sun produce the necessary currents to generate the over all magnetic field, which oscillates from a polar geometry to a donut-shaped toroidal geometry every 22 years with a change in polarity. The Parker Theory seems to do a good job in explaining it. Ultimately, currents produce magnetic fields on the Sun just as they do in the lab, but the current flows are a bit more complicated, as is the medium in which they flow.