Thunder is produced mainly within cumulonimbus clouds. Cumulonimbus clouds contain very small ice particles and relatively large hail. When they collide, the small ice particles are electrified positive ions and the hailstones are electrified negative ions.
The lighter small ice particles with positive charges gather above the cloud, and the heavier hail with negative charges gather below the cloud, so the negative charges at the bottom of the cumulonimbus cloud attract and the positive charges accumulate on the ground below.
Between the clouds and the ground when a certain amount of electricity accumulates, light and sound discharges are produced, thunder is heard and lightning is seen.