https://en.wikipedia.org/wiki/Explosive

"Explosive materials may be categorized by the speed at which they expand. Materials that detonate (the front of the chemical reaction moves faster through the material than the speed of sound) are said to be "high explosives" and materials that deflagrate are said to be "low explosives"."

Note that the relevant speed of sound here is the speed of sound in the material, not the speed of sound in air at STP. Once the reaction rate goes supersonic, the pressure rise becomes very sharp, like a shock wave in front of a supersonic jet. The very sharp pressure rise means that there's very little force pushing/scattering the explosive before there's enough pressure to crush the explosive sufficiently to cause it to react, so momentum is sufficient to contain the explosive long enough to get most of it to react, without the need for any container to hold it in place.

Without detonation, burning ammonium nitrate would have had a hard time building up pressure higher than the bursting pressure of the windows on the warehouse. Sure, the bursting pressure of the widows of the warehouse is high enough to break nearby windows and do damage, but orders of magnitude less damage than seen in this case. In the case of a deflagration, the generated overpressure is usually limited by the strength of the container (in this case, the warehouse). In a detonation, the reaction rate is such that the solid/liquid becomes a very hot gas essentially in-place (most secondary high explosives have very few solid reaction products).

There's basically a hierarchy of tendency to transition from normal burning (deflagration) to detonation (supersonic flame front): primary high explosives, secondary high explosives, insensitive high explosives, low explosives. Low explosives are difficult or impossible to get to detonate. Primary high explosives are typically the most dangerous, because they easily detonate once they start burning. (Also, the sort of instability for easy deflagration-to-detonation transition also correlates with instability when struck/dropped/crushed. The most common commercial primary explosives are lead azid and mercury fulminate, which obviously also throw around toxic heavy metal compounds.)

Insensitive high explosives are used in things like weapons that are designed to travel through brick walls or steel armor and explode on the other side. In such a case, (in the pre-electronic age), you'd have something like a momentum-based striker that strikes a percussion cap when the weapon makes impact, igniting a low explosive delay element (timing train) made out of something like solid pressed black powder, which would burn for a predictable period of time before igniting a small amount of primary explosive (e.g. lead azide). The primary explosive would start out deflagrating, but it easily undergoes deflagration-to-detonation transition, and detonates. The detonation shock wave from the primary high explosive sets of a small amount of secondary high explosive (say, PETN), which gives enough kick to set of the main charge of insensitive high explosive (such as ammonium picrate).

The compression of nuclear weapon fission primaries is usually done via insensitive high explosives that are also resistant to radiation degradation. Even though accidental detonation in a crash/drop would unlikely to cause a nuclear explosion, spreading toxic radioactive plutonium everywhere is still Very Bad (TM). I'm not a chemist, but I would guess that chemical stability against radiation degradation also correlates with pressure/temperature stability of insensitive high explosives.

During the Vietnam war, supposedly soldiers would sometimes burn C4 plastic explosives to heat food. C4 is basically RDX mixed with just enough polymer to make it into a pliable putty. RDX is very unlikely to undergo DTD transition, so this was relatively safe-ish, and RDX is chemically similar to hexamine solid fuel tablets used in some camping stoves. Dynamite is also a secondary high explosive, but nitroglycerine-containing dynamites are significantly less safe to burn than RDX.