You would expect that the farther away you got from a hot body like the Sun, that the cooler the surrounding gases would become. The solar interior is a nuclear furnace at a core temperature near 15 million degrees. By the time you reach the solar surface, a region called the photosphere, the temperature has diminished to about 5700 degrees on the Kelvin scale. Then something peculiar happens. As you move away from the photosphere and enter the next outer layer called the chromosphere, the temperatures begin to climb to over 10,000 Kelvins, and by the time you enter the solar coronal regions some 300,000 kilometers above the photosphere, the temperature of the solar gases has climbed to over 1 million degrees!
Although solar physicists do not fully understand the reasons behind this, they have some pretty good ideas. First, the gases in the outer layers of the Sun are ionized, a condition called a plasma. Plasmas, and charged particles, are easily influenced by magnetic fields, and the Sun has a variety of these found in sunspots and other areas. When these magnetic fields get tangled up, they can explosively reconnect to produce tremendous instantaneous currents which get dissipated ( just like the currents in an ordinary wire!). This 'Ohmic heating' causes the plasma involved to reach very high temperatures, and we see these events as solar flares. In the chromosphere, there are lots of magnetic storms, spicules, flocculi ,flares, prominences etc, which magnetically stir up the solar plasma to high temperatures. This is why the chromosphere can have a higher temperature than the underlying layers where magnetic field reconnection may be less effective in heating.
The coronal temperature may require another explanation. When you crack a whip, you do this by gently shaking one end and watching as this disturbance moves down the tapered whip to its end where an amplification of your energy causes the tip of the whip to move at supersonic speeds. The same thing may happen in the corona. A small input of energy at the base of the corona, in the chromosphere for instance, gets amplified as it travels down the decreasing density gradient in the corona. Because energy must be conserved even as the density of the gases diminish, the gases farthest from the base of the corona must travel at higher and higher speeds, heating the plasma to increasing temperatures.
There is still much we do not know about the Sun, how energy flows from its surface, and many other exciting details. But we are understanding more and more every year. Stay tuned!!