As magma rises and the ambient confining pressures drop, the partial pressure of the gases fall and they begin to come out of solution in the magma. As a result the gases form bubbles and then expand. This froths the magma and agitates the entire top of the magma column or chamber. If the gases expand too quickly, and the ambient pressure drops rapidly the gases can explode from the magma. This can happen when the magma rises very quickly from deep in the crust - the gas doesn't escape slowly, it rapidly comes out of solution and forms bubbles which accumulate rapidly and explode outwards, potentially destroying the edifice.
The phenomenon is often compared to opening the top on a warm bottle of carbonated soft drink - the sudden pressure release triggers the rapid exsolution of the gas which can burst out from the bottle.