Lightning is a huge electrical discharge (a giant spark) either between one cloud and another or between a cloud and the ground. I have seen a thunderstorm where the whole sky was lit up continuously for over an hour by flash after flash between clouds.
Lightning is formed because of a build up of electric charge in a cloud. In a storm there are enormous convection currents in a cloud, water droplets and ice particles going up and down within it. This movement causes friction between the particles when they collide which charges up the cloud. Positive and negative charges separate and an electric field is formed.
When the charge is big enough the electric field ionises the air. The electrical resistance of the air 'breaks down', it is no longer an insulator and the charge is discharged as a spark.