The sun's magnetic field changes polarity approximately every 11 years. It happens at the peak of each solar cycle as the sun's inner magnetic dynamo re-organizes itself.
"The changing magnetic field is described by dynamo models: the rotation takes effect on the convective envelope and the result is the surface differential rotation, when the angular rotation has an equator-to-pole gradient. Differential rotation acts on the primordial poloidal field, and starts to build up a toroidal one (see the figure). Under the surface, somewhere at the bottom of the convective zone, this spin up effect streches, thus reinforces the toroidal field until it becomes an unstable state, when the buoyant force begins to act on them. Thus, the toroidal magnetic field arranged in vast flux tubes begins to emerge and reaches the solar surface. At this time, a typical magnetic feature appears: a magnetic loop, with a cool bipolar spot pair at the footpoints. The large scale meridional convection works just the opposite: somehow (that we do not understand clearly) it regenerates the poloidal field, transporting the emerging flux towards the poles. Finally, this process results in a poloidal field, similar to that we had at the beginning, but the polarity is now the opposite in sign."Source: http://www.konkoly.hu/solstart/stellar_activity.html