The wikipedia article is quite good on this subject.
For any discharge in the air the molecules of the air must be ionized. This ionization happens during thunderstorms because of the high static electric fields carried by the clouds which generate "streamers", i.e. paths for the electrons to flow downwards. Corresponding streamers are formed by conductors and high points on the ground with positive charge again generated by the high fields of the storm cloud, the positive ions flow upward and the path for a discharge is set.
A lightning flash terminates [& discharge occurs] on a tree while an un-attached streamer is visible on the earth surface projection to the left.
When the electric field of the storm passes over the ground, high points that are also grounded have higher fields then the ground and can form streamers. Lightning rods work, by generating upward streamers more efficiently since they are highly conducting and the field at the top is very high due to the geometry.
When a stepped leader approaches the ground, the presence of opposite charges on the ground enhances the strength of the electric field. The electric field is strongest on grounded objects whose tops are closest to the base of the thundercloud, such as trees and tall buildings. If the electric field is strong enough, a positively charged ionic channel, called a positive or upward streamer, can develop from these points. This was first theorized by Heinz Kasemir.
As negatively charged leaders approach, increasing the localized electric field strength, grounded objects already experiencing corona discharge exceed a threshold and form upward streamers. Once any downward leader connects to any upward leader available, a process referred to as "attachment", a circuit is formed and discharge may occur. Photographs have been taken on which unattached streamers are clearly visible. The unattached downward leaders are also visible in branched lightning, none of which are connected to the earth, although it may appear they are.
Once a channel of ionized air is established between the cloud and ground this becomes a path of least resistance and allows for a much greater current to propagate from the Earth back up the leader into the cloud. This is the return stroke and it is the most luminous and noticeable part of the lightning discharge.
When the electric field becomes strong enough, an electrical discharge (the bolt of lightning) occurs within clouds or between clouds and the ground. During the strike, successive portions of air become a conductive discharge channel as the electrons and positive ions of air molecules are pulled away from each other and forced to flow in opposite directions.
The electrical discharge (averaging 30 kA for negative or 300 kA for positive lightning, and travelling at around 1×108 m/s) rapidly superheats the discharge channel, causing the air to expand rapidly and produce a shock wave heard as thunder. The rolling and gradually dissipating rumble of thunder is caused by the time delay of sound coming from different portions of a long stroke
It is wise not to be swimming during a thunderstorm because the water being flat the most discontinuous conductive object will be the head. Also if during a thunderstorm in the open air one's hair becomes electrified it is wise to fall on the ground and keep away from high objects like trees.