Once upon a time the sun was not there, not anywhere. It did not exist.
Five billion years ago what was to become the sun was a vast cloud of very thin gas, mostly hydrogen and helium from the big bang. There were also heavier elements like carbon, oxygen, iron, gold and all the others; they came from stars that collapsed and then exploded, flinging those atoms across space. That rarefied cloud was bigger than what became our solar system and was surrounded by a more perfect vacuum. Slowly it contracted under its own gravity.
Over a few hundred million years most of that gas collapsed down to become the sun, with a little left outside that became Earth, the other planets and their moons. The collapsing gas cloud heated up, the way squeezing the air in a bicycle pump gets warm – reduce the volume and the temperature rises. It was once thought that this “gravitational collapse heating” might be the reason the sun is hot, but that wouldn’t keep it hot for long enough.
Then in the 19th century, scientists wondered if the sun’s heat came from burning fuels like oil or gas. But a calculation showed that it could not stay hot for more than a few million years. No problem if the Earth was only thousands of years old, but geologists, and Darwin, too, reasoned that the Earth had to be at least a billion years old. Since a billion is a thousand million, that was a problem.
The problem was solved early in the 20th century. Burning fuels releases chemical energy, the energy of electrons orbiting the much tinier atomic nuclei. Those nuclei contained vastly more energy if only it could be released. It could. Some of the hydrogen, whose nucleus is a single proton, was “heavy hydrogen” – nuclei with a neutron stuck to the proton, called deuterons. Most of those had also been made in the big bang. Under gravitational collapse, the sun’s center became so hot and dense that pairs of deuterons stick together making nuclei of helium (2 protons and 2 neutrons); some mass disappears in the process. Einstein had shown that mass and energy are equivalent (You know, the famous equation: “E = mc2”) so the disappearing mass becomes energy.
So, the sun is a giant fusion reactor, steadily converting hydrogen to helium, a safe distance from us. Et voila, the problem of the sun’s energy source was solved!