Dear student
Initially electric charges are formed in the atmosphere mainly due to the ionisation of air molecules by cosmic rays. Cosmic rays are high-energy particles such as protons that originate from outside the solar system. On colliding with air molecules, they produce a shower of lighter particles, some of which are charged.As the area of negative charge at the base of the thundercloud builds up, it induces a region of positive charge to develop on the ground below.
As a result of this, a potential difference or voltage is created across the cloud-to-ground gap which is reason for lightening
With regards