Hi.
The processes that go into generating a magnetic field are a bit complex and we don't understand everything happening on the Sun, but basically you can generate a magnetic field if an object is rotating, contains a conducting fluid, and has a bit of a magnetic field to start with. The Sun fits these criteria - it is a giant rotating ball and its outer third or so is made up of flowing plasma (basically a a magnetized fluid), and we think there was a bit of a magnetic field in the initial gas from which the Sun was formed. There is a lot more about the Sun's magnetic field here:
http://sunearthday.gsfc.n.../TTT/60_magfield.php
Other stars also show magnetic activity, and the ones that rotate faster or are thought to have deeper layers of freely moving plasma inside show more activity.
The Earth also has a magnetic field because it rotates and has it has a layer of liquid iron flowing inside it. (see http://geomag.nrcan.gc.ca/mag_fld/fld-eng.php and http://geomag.usgs.gov/faqs.php#six).
Terry