During a storm, collisions of water and ice particles in the clouds can result in a charge separation within the clouds, resulting in the buildup of significant electric fields. If the fields become large enough, lightning may occur between the cloud(s) and the ground, or between different clouds, which reduces the charge separation. The lightning in the sky is just a very large spark, not all that different from how you might occasionally get a small shock from static electricity when you touch a doorknob.