The easiest way to think of the sun is as a giant ball of very hot plasma, composed of around 73% of hydrogen, 25% helium, and the remainder being elements such as oxygen, iron, neon, nitrogen and silicon.
So how does the sun produce energy from this diverse mixture of elements?
At its centre, gravity causes hydrogen atoms to collide together at such great temperature and pressure that they fuse to form atoms of helium. This is known as nuclear fusion, a process whereby a single heavy nucleus is formed from combining two lighter nuclei. In this case two hydrogen nuclei fuse together, becoming heavier than either hydrogen nuclei but not as heavy as the combined mass of the two original nuclei.
This difference in mass is transformed into energy, which travels outward to the surface from the core of the sun. The journey can take over 100,000 years to complete.
The resulting sunlight takes just over eight minutes to reach earth, and is best thought of as electromagnetic radiation comprising of infrared, visible, and ultraviolet light.
Short answer: The sun creates energy via nuclear fusion, whereby energy is released as a byproduct of hydrogen atoms fusing into helium.