Inside a thunderstorm, the movement of rain and ice creates a separation of electrical charges, with the negative charge (electrons) forming at the bottom of the cloud and the positive charge (protons) forming at the top.
Have you ever rubbed your feet across carpet and then touched a metal door handle? Did you get shocked? Lightning works in the same way. Source: Lightning | WikiMedia
Since opposites attract, that causes a positive charge to buildup on the ground beneath the cloud. Lighting is the electricity produced when the two charges eventually connect. When the air heats rapidly due to lightning, it quickly expands, creating an acoustic shock wave that we call thunder. If you are close enough to hear thunder, you are close enough to be struck by lightning.