Lightning occurs when the negative charges come near enough to an attractive positive charge.
As the atmosphere heats and cools, it expands and contracts, causing changes in pressure and air movement.
Water droplets inside clouds have a positive electrical charge at the top of the cloud and a negative charge at the bottom.
When the negative charge comes near enough to an attractive positive charge on the Earth below or on another cloud, the electrical energy is released in a flash of light.
There may be a loud band, called thunder, at the same time.
Thunder is caused by the air being heated to a tremendous temperature, and the explosive noise is when it expands suddenly.
However, as light travels faster through the air than sound, we see the lightning flash before hearing the thunder.