We know that sunspots are magnetic because we can directly observe the effect that the field has on the spectral lines we observe through a phenomenon called the Zeeman effect.
Let's back up a little bit and go through this step by step. First, by "spectral lines" we mean the sets of discrete energies at which different elements in the Sun emit and absorb. When you look at the intensity of light that is emitted at each wavelength (that's called a "spectrum"), you get something like Fig. 1 at this link. In the above picture, the dashed line represents what astronomers call the "blackbody" spectrum of the Sun, which is just the ideal energy distribution that the Sun emits because of its surface temperature. The solid line is the actual spectrum observed; the difference between the two is caused by the spectral lines (sharp intensity dips/rises at specific wavelengths): the discrete transitions of the atoms and molecules in the Sun's photosphere "eat away" at the smooth spectrum. This next picture shows the Sun's spectral lines a bit better (the blackbody spectrum has been subtracted here): wavelength is labelled along the horizontal direction, and the dark vertical bands that you see are "absorption lines", or absorption by specific atoms at specific wavelengths.
Now, spectral lines are great for a number of reasons. First, each element has its own spectral line "fingerprint": in other words, we can determine what elements are present in the Sun by looking for the specific combination of spectral lines that corresponds to that species. Next, the exact wavelength at which the spectral lines fall depends on the velocity of the Sun's atmosphere relative to us, along our line of sight: that means that we can track motions in the solar photosphere by looking for wavelength shifts in the spectral lines! Lastly, some of the lines that you see actually split into two or more lines when the elements that cause the lines are in a magnetic field; this phenomenon is called "Zeeman splitting". A measurement of the extent of the splitting therefore tells us about the magnetic field.
Finally, to answer your question: astronomers know that sunspots are associated with magnetic fields because they can look at the spectral lines that come from the sunspots and measure by how much these lines are split. Using what we know about how magnetic fields affect the lines, they can compute the strength of the field required to reproduce the splitting that they see. These computations indicate that the magnetic field in the sunspots is much stronger than that on the rest of the Sun's surface, and thus that sunspots are somehow related to the Sun's magnetic field.
Last updated Jan 28, 2019