In thunderstorm development, the rising air called updrafts keeps various forms of frozen precipitation suspended within the cloud. Lighter ice crystals are carried upward to the top of the cloud, while heavier and denser pellets are suspended by updrafts or fall to the ground as hail.Collisions of these ice crystals and pellets become the charging mechanism needed for lightning.The ice crystals become positively charged and the pellets become negatively charged, so the top of the cloud becomes positively charged and the bottom becomes negatively charged.Meanwhile, the ground underneath the cloud becomes positively charged.
When the difference in charge gets large enough from the bottom of the cloud to the ground, asmall amount of charge (called a step leader) is sent out toward the ground. When it nears the ground, it attracts all the positive charge in the area, which start sending out small bolts of ground to air lightning (called streamers).If the streamers make contact with the step leader, a discharge occurs between the cloud and the ground and we see this as a bright flash of lightning!
Image Credit: NWS How Lightning is Created
Cloud to cloud lightning acts similar as cloud to ground, where the opposing charge builds up and requires a discharge with the negative charges in one cloud reaching out to the positive charges of another cloud (or the upper levels of the same cloud).
Image Credit: Weather Underground: Lightning Preparedness