In ancient times, people looked up to the sky and thought it was the home of the gods; they found a divine explanation for pretty much all natural phenomena, and thunder was usually a tool of the gods. Strange enough, but today things aren't really that different. Sure, you'll say, we know it's a discharge of electricity and not the hammer of the gods, but I was surprised to find out that most people don't know much more about it.
Back in the 1700s, Benjamin Franklin started studying lightning, and he proved his groundbreaking results with the famous Philadelphia spire experiment; he showed that sparks are similar in many ways to lightning. His kite experiment is even more famous, despite the fact that he wasn't the first to conduct it.
Lightning is formed in a pretty straightforward way (except from what is called positive lightning, which makes less than 5% of all lightning; we'll deal with that a bit later). First, falling droplets of ice become electrically polarized passing through the atmosphere and these colliding ice particles become charged through induction.
This is still subject to debate, but it's currently the most accepted theory (still, it is a theory). The smaller particles become positive by losing negative ions while the larger ones gain negative ions and become negative. As the thundercloud moves around the surface of the earth, the portion of the earth beneath it is induced with an equal and opposite charge. In the first phase of the lightning, a relatively small current (10s or 100s of ampers) is created.
When the electric field becomes strong enough, an electrical discharge (the bolt of lightning) is created, occurring between either different clouds or between a cloud and the ground beneath it. Electrons and positive ions of air are forced to go opposite ways, basically ripped apart.
Thunder is a result of lightning. When the electrical discharge takes place, the discharge tunnel is superheated, causing the air to expand very quickly. Since this air moves quicker than the speed of sound, a shock wave is created, and then slowly dissipated. The resulting sound is thunder.