Contact and separation between materials can transfer electrons. Your body may gain or lose a small amount of charge. The number of transferred electrons can be enormous even though the net charge is tiny compared with all the charge already present.
As charge separates, electric potential energy increases. The electric field across the small air gap near the doorknob becomes strong enough to ionize air molecules, making a conducting path. Charge rushes through it as a spark.
A thundercloud is vastly larger, but the logic is related: separated charge creates intense electric fields. When air can no longer insulate, a branching channel forms and an enormous discharge follows.