Click below for some interesting and informative videos and links about the history of MIL Spec foam and how it mitigates slosh while acting as an explosion suppressant.
As the fuel level drops in the tank, the volume of the tank where fuel once occupied is now occupied with ignitable fuel vapors. If an event occurs, such as a projectile hitting the outside of tank, a lightening strike and/or an ignition spark inside the tank, these vapors can possibly ignite. Once ignited, there is nothing to stop this until an explosion occurs.
Same situation but now the foam replaces the volume where the fuel once was and is now in the area of the ignitable fuel vapors. If an ignition event occurs, the foam acts as a heat sink, removing the energy from combustion spreading.