In my opinion, we know the temperature of the sun in two ways: theory and observation. Theoretically, we can estimate the temperatures of various solar layers by considering the underlying physical processes.
Observationally, we can directly measure the temperatures of the layers above the photosphere (including photosphere, chromosphere, transition region, and corona) either with remote telescopes (we can derive the temperatures based on spectroscopic data) or with in-situ instruments onboard spacecraft (a method applies only to the solar corona when Parker Solar Probe enters it).