tl;dr: Lightning heats the air around it as it strikes. Thunder is the sound created when the heated air expands explosively, then contracts rapidly as it cools.
Explanation:
To understand what thunder is, we need to explain lightning. Lightning starts to form when static begins to charge in a rain cloud. Since the winds inside the cloud are very turbulent, water droplets in the bottom of the cloud are caught in updrafts. They are then lifted upwards to where much colder atmosphere freezes them. Simultaneously, downdrafts in the cloud push ice and hail down from the top of the cloud. As the ice meets the water coming up, electrons are stripped off.
This creates a cloud with a negatively charged bottom, a positively charged top, and the atmosphere acting as an insulator in between. Once the strength of the charge becomes so great and overpowers the insulating properties of the atmosphere, lightning happens.
“Okay, but why does lightning strike the ground?”
Most often than not, lightning occurs within a cloud or between clouds. However, lightning can and will also strike the ground. The strong negative charge in the cloud attracts the positive charges in the ground. These positive charges move up into the tallest objects (hence the use of lightning rods). Once a negative charge descends from the cloud, a positive charge called a streamer, reaches up to meet the negative charge. They both connect and that is when we see the lightning occur.
“Great, so how is this related to thunder?”
In just a fraction of a second, lightning will heat the air around it to as hot as 54,000 degrees Fahrenheit. That is roughly five times hotter than the surface of the Sun.
The heated air expands explosively, as the surrounding air is compressed. The air then contracts as it cools. The air expanding explosively creates the initial crack of lightning, followed by the rumbling as the air molecules vibrate.
“Is it true you can estimate how close lightning is?”
Yes! We see lightning before we hear thunder because light travels much faster than sound waves. We can estimate the distance of the lightning by counting how many seconds it takes until we hear the thunder. It takes approximately 5 seconds for the sound to travel 1 mile.