It's caused by the sun's rotation, mostly. There are some complex magnetics that go on inside the sun, but the gist of it is that there is a differential in rotation rates between the poles and the equator of the sun. This causes field lines to be dragged along the equator, which then get knotted up and form coronal holes and other phenomena. Eventually, along an 11-year cycle, the field flips orientation.
Source: I work in a solar research lab at the University of New Hampshire
Source: I work in a solar research lab at the University of New Hampshire