The main point of this answer is to use the 2013 West Fertilizer Company explosion (USCSB animation for context) as an example of scenarios that could lead to AN detonation, and also to show that the situation can become very complex and unpredictable.
Anything in the vicinity can become fuel, especially if a fire is already in progress. This includes the containers, impurities, soot and debris from the fire, etc. Plus, ammonium nitrate's melting point is ~337 F, meaning it can become molten, possibly escaping its container, and readily mixing with fuel sources.
The US CSB West Fertilizer Explosion final report, section 4.3, outlines three possible scenarios under which the 2013 explosion in West, Texas could have occurred. Section 4.2 outlines general contributing factors.
These aren't the only ways it can explode, but they are a few examples of the types of conditions that could lead to explosion.
You should definitely read the report; my brief summary below leaves a lot of relevant analysis out.
So from section 4.2, contributing factors (FGAN = fertilizer grade ammonium nitrate):
Contamination
In fire situations, the behavior of FGAN is unpredictable, in part because of the number of endothermic
and exothermic decomposition reactions that take place with increasing temperature. FGAN
decomposition reactions beyond the first step have yet to be uniquely defined, and subsequent
decomposition reactions of FGAN can only be assumed. When contaminants are added to AN, the
decomposition reactions become increasingly more complex. Possible sources of contamination in an
FGAN storage area can include ignitable liquids, finely divided metals or organic materials, chloride
salts, carbons, acids, fibers, and sulfides. These contaminants can increase the explosive sensitivity of
FGAN.
The molten FGAN at the WFC likely came in contact with contaminants that were stored in the fertilizer
warehouse or were produced during the fire that preceded the explosion. Seed materials, zinc, and other
organic products, including the wood-constructed bins, were present near the FGAN storage area or could
have come in contact with molten FGAN. During the fire, soot from the smoke and also collapsing wood
and roofing material might have mixed with the FGAN pile.
Poor Ventilation
The limited ventilation increased the quantity of soot in the smoke and the potential
contamination of the FGAN pile. ...
At some point around 5 to 6 minutes before the detonation, the character of the fire changed, according to
eyewitness accounts and photographic evidence (Figure 40). This change was most likely caused by
increased ventilation through an opening low in the building, possibly when the fire burned through the
seed room doors or the roof. The fire also might have been enhanced by oxidizing gases from the heated
FGAN pile...
The additional ventilation caused a marked decrease in dark smoke and probably was accompanied by a
major increase in heat radiation inside the fertilizer building because of increased oxygen availability to
the burning wood and other fuels. With the dark smoke inside of the structure reduced, radiant heat
would reach the surface of the FGAN in the bin, and the increased airflow through the building would
greatly increase the radiant heat flux by raising the temperature of the burning wood. The surface of the
FGAN, covered with soot or molten asphalt, would absorb the heat flux and cause a very rapid heating of
the surface of the FGAN pile. The very hot and contaminated surface of the pile was then sensitive to
detonation.
And from section 4.3, a few detonation scenarios:
- Scenario 1: Detonation from the top of the FGAN pile.
- Scenario 2: Detonation in heated FGAN along exterior wall exposed to fire.
- Scenario 3: Detonation in elevator pit that spread to main FGAN bin
Scenario 1: Detonation from top of pile
Based on the location of the pile and the properties of the bin along with the circumstances of other fire induced incidents, one possible scenario is that a period of contamination with soot and other organics
(possibly including molten asphalt and plastic dripping from the burning composite shingle roof and PVC
drop pipe from the elevator mechanism) was followed by about 5 to 6 minutes of intense radiant heating
from the flames above and adjacent to the main FGAN bin. During this time, a layer of very hot,
contaminated, and sensitive liquid FGAN could have built up on the pile. The foaming FGAN likely
produced oxidizing gases, and those mixed with flammable smoke to produce a detonable gas cloud over
the FGAN pile in the main bin and possibly in an adjoining bin linked to the main bin through a series of
holes cut in the partition between the bins. The cloud consisted of powerful oxidizers that would be
expected when FGAN undergoes thermal decomposition—such as NO2, O2, and HNO3 as wells as fuelrich smoke and pyrolysis products off-gassing from the molten FGAN. The gas cloud then might have
ignited from above, undergoing a gas-phase deflagration-to-detonation transition (DDT) in the
confinement of the bin.
Scenario 2: Detonation along fire line
This scenario involved heating of the FGAN through the walls and is noted as being highly unlikely, so to keep this short I'm not going to quote it here. See section 4.3.2 for details.
Scenario 3: Detonation in elevator pit
Another possible detonation scenario focuses on the elevator pit near the FGAN bin. A fiberglass lid
covered the pit, and the floor sloped away from the pit to prevent runoff from entering it, but the fire
might have melted the cover, and FGAN remnants could have been in the pit. ...
If the
detonation began in the pit, then the most feasible mechanism would be a collapse of the west wall of the
bin, spilling FGAN into a mixture of burning rubber from the melted elevator belt and residual FGAN in
the bottom of the pit. The mass of the falling FGAN, combined with the strong confinement of the
concrete pit walls, might have provided the conditions for a solid phase DDT beginning in the bottom of
pit and spreading into the main pile.
TLDR
So yeah, the TLDR here is that conditions in a fire can be extremely complicated and unpredictable, giving rise to a lot of opportunities for contamination and detonation.
In particular:
- AN can melt and the liquid can do unpredictable things.
- The containers can be destroyed by fire allowing AN to escape into unpredictable places.
- Even if the AN was contaminate free under normal conditions, anything in the area can become a fuel source including the containers themselves, fire debris, smoke, soot, collapsed building parts, etc.
In Beirut we saw that there was already a fire burning for a significant amount of time before the explosion, as well as a smaller explosion that occurred < 30 seconds before the main one. There were also flashes and bangs and a lot of other stuff going on there (reportedly there were fireworks stored in the same warehouse). It was at a seaport, too, meaning there were probably a lot of nearby things to act as fuel sources.
It is very, very conceivable that the AN became both heated enough and contaminated enough during this time to detonate.
Here is a list of other AN accidents that you could research on your own to find out about other scenarios that can lead to detonations. Most notable:
- BASF, Oppau, Germany, 1921
- Texas City, Texas, USA, 1947
- AZF, Toulouse, France, 2001
- Ryongchŏn, North Korea, 2004
- Tianjin, China, 2015
Also, you may find some of the theories regarding the 1988 PEPCON accident in Nevada, USA interesting as well. That was not ammonium nitrate (it was ammonium perchlorate, another oxidizer), but the possible scenarios are similar and it also illustrates the complexity of those kinds of situations.