Electric currents inside the sun generate a magnetic field that spreads throughout the solar system. The field causes activity at the surface of the sun, surging and ebbing in a regular cycle. At the peak of the cycle, the polarity of the field flips, during a time of maximum sunspot activity.
The sun's magnetic field has two poles, like a bar magnet. The poles flip at the peak of the solar activity cycle, every 11 years. A solar wind composed of charged particles carries the magnetic field away from the sun’s surface and through the solar system.
The sun is not a solid ball, but rather like a fluid. It exhibits differential rotation, meaning the surface moves at different speeds depending on latitude. This results in the magnetic field lines getting wound up. When the winding gets extreme, the magnetic field lines "snap," causing solar flares at those locations on the surface.
The sun's magnetic influence extends well past the planets and into interstellar space. This region, called the heliosphere, acts as a magnetic shield against charged particles from deep space called cosmic rays.