This is how Wikipedia explains stellar magnetic fields:
Stellar magnetic fields, according to solar dynamo theory, are caused within the convective zone of the star. The convective circulation of the conducting plasma functions like a dynamo. This activity destroys the star's primordial magnetic field, then generates a dipolar magnetic field. As the star undergoes differential rotation—rotating at different rates for various latitudes—the magnetism is wound into a toroidal field of "flux ropes" that become wrapped around the star. source
So this basically says that the solar magnetic field is driven by heat convection which makes charged particle flows which create the solar magnetic field by magnetohydrodynamics. The magnetic polarity of the Sun shifts every 11th year, because flux ropes are thought to become wrapped around the Sun.
But really we don't know the details on how the solar dynamo works, as Wikipedia states:
The detailed mechanism of the solar dynamo is not known and is the subject of current research. source
It does not get any better if we look at the observations. We expected massive heat convection inside the Sun, driven by the temperature difference between the fusion powered 16 million degree hot core and the huge temperature difference to the 5800 Kelvin hot photosphere. But then the science of helioseismology came along and managed to interpret solar seismic waves and map the solar interior, and found no solar heat driven convection inside the Sun at all.
The first things helioseismology noticed was that most of the solar interior rotates as one solid sphere, beneath the tachocline, without any observable heat convection. So they had to update the solar model and put the convection zone outside of the "solid" sphere:
But recently the measurements have become so good that we can even map the flows in the convection zone, and we have found a rather tranquil "weather pattern", a double celled weather pattern. And this weather pattern is not driven by heat convection, and heat convection is rather thought to slow the weather pattern down:
The Sun’s meridional circulation is most likely mechanically driven and thermally braked, roughly opposite to the driving mechanism of the Hadley cell in the Earth’s atmosphere source
It seems like we don't know the details on how the solar dynamo theory works, and the solar dynamo theory even seems to be in conflict with the observations. Can we then claim that we know how the magnetic field of our Sun is created?
The magnetic field of the Sun shift magnetic polarity every 11th year. The current explanation on how this could be done is magnetic flux lines becoming magnetic flux ropes that winds up around the Sun due to difference in rotation, as the photosphere rotate faster around equator than around the poles. This somehow diminishes the existing magnetic field, and folding or fluxrope breaking is thought to reverse the magnetic polarity of the Sun. The details of this theory are neither well understood and is also a subject of current research.
It seems like stellar magnetic theory is in trouble, but there neither seems to be any good alternatives to research. If it only was as simple as the geomagnetic field. Earth has a solid inner iron/nickel core which can hold a permanent magnetic field, and our planets core has in geological history changed magnetic polarity many times. That is impossible at the Sun, as according to our solar model, the core is a 15 million degree hot plasma fusion furnace, and the observations of solar neutrinos confirms that the Sun is fusion powered. We could try things like moving all solar fusion to the millions of degrees hot solar corona, but that would be a violation of the current solar model. The solar corona do have a lot of magnetic reconnection in its hotspots, magnetic reconnection has spoiled many fusion experiments on Earth, but now it seems like they are starting to use magnetic reconnection to create fusion:
In fusion facilities, reconnection can help start and confine the plasma that fuels fusion reactions. source
But such a suggestion is just topsy turvey, or is it?