In a “normal” atmosphere temperatures cool with height therefore, the sound from thunder escapes up into the atmosphere.
When temperatures rise with height through the atmosphere, this is called a temperature inversion (the red line is the Temperature, the green line is the Dewpoint). Here is an atmospheric sounding (temperature/humidity profile) of the airmass over Greensboro, North Carolina Monday, February, 15th.
When there is thunder and a temperature inversion is in place, this causes “elevated thunderstorms”. This happens because the temperature inversion acts as a lid over the surface. Since the sound cannot escape up into the atmosphere, the sound bounces between the ground and the warm layer aloft.