If light only travels in a straight line, why doesn’t lightning?

The simple answer is that it is because lightning isn’t light, it’s electricity. Lightning is called ‘lightning’ because of the extremely bright light which it creates and lights up the sky.

Lightning is an enormous electrostatic discharge between the cloud and the ground, other clouds, or within a cloud. Electrons follow the path of least resistance as it needs the least amount of energy, and can follow any shape.

Cloud-to-ground lightning begins with the water cycle where the heat from the sun causes moisture to rise in the form of vapour, forming clouds. Storm clouds become positively charged at the top and negatively charged at the bottom due to up drafts, down drafts, freezing and particle collisions. As the charge separation grows stronger, so does the associated electric field.

Eventually this can cause the air around the cloud to become ionised or “break down”, allowing current to flow through the ionised air and potentially neutralising the charge separation, called a step leader. At the same time, the positive charge is growing on the Earth’s surface, and objects (including people) respond to this by sending out positive streamers. When a step leader and a streamer meet, they can form a complete path for lightning to travel from the cloud to the ground, and the meeting results in the occurrence of lightning.

Following this, thunder takes place when energy from the lightning strike heats the air to about 18,000 degrees Fahrenheit, causing an expansion that creates a sound wave that radiates away from the strike path.

The main reason is due to the non-uniform air; our atmosphere has many particles such as dust, ice and variations in humidity, density,etc. And lightning tries to find the path of least resistance; the one with less obstacles.
Digging more deeper, the electrons in the lightning strike can move in a straight line in vacuum; where there is absolutely no obstacles and variations. This phenomenon of lightning is called “tortuosity” . You could search more about the phenomenon, it is interesting!
Reference: https://physics.stackexchange.com/questions/136694/why-is-the-shape-of-lightning-or-an-electric-spark-a-zig-zag-line