How is the solar magnetic field generated?

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 Solar Dynamo Theory developed in the 1950's, 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. For more informaton about the solar magnetic dynamo, visit the NASA Marshall Space Flight Center information page on this topic.