Lightning is an electric current. This electric current is formed inside a cloud.
When the ground is hot, it heats the air above it. This warm air rises. As the air rises, water vapour present in the air cools and forms a cloud. When air continues to rise, the cloud gets bigger and bigger. In the tops of the clouds, temperature is below freezing and the water vapour turns into ice.
Now, the cloud becomes a thundercloud. Lots of small bits of ice bump into each other as they move around. All these collisions and mutual rubbing causes a build up of electrical charge.
Eventually, the whole cloud fills up with electrical charges. Lighter, positively charged particles form at the top of the cloud. Heavier, negatively charged particles sink to the bottom of the cloud.
When the positive and negative charges grow large enough, a giant spark - lightning - occurs between the two charges within the cloud.
This is how a lightning is formed.