Thunder is caused by lightning, so it cannot happen independently. The loud cracking sound of thunder occurs when lightning raises the surrounding air temperature by thousands of degrees, causing it to rapidly expand and compress, according to SciJinks.
Within a fraction of a second, lightning is capable of heating the air as high as 54,000 degrees Fahrenheit, SciJinks states. After being heated and compressed, the expanded columns of air shrink and cool down, giving off the initial "clap" of thunder. The air columns continue to vibrate for a short time, releasing the residual sound waves of thunder. Occasionally, lightning appears without a thunder clap because the sound wave occurred too far away to be heard.