Acoustic foam is made of a rubbery, flexible resin in an open-celled (reticulated) network. Sound waves that strike it can easily penetrate it because it is mostly air. The air motion associated with the sound wave causes the resin webbing to flex very slightly, and internal friction in the resin then dissipates the flexure energy as heat.
(In addition, for "small" pore sizes (~ 0.1 millimeter) there are viscuous losses incurred as the air moves back and forth through them; this also contributes to attenuating sound waves traveling through acoustic foam.)
By carefully engineering the stiffness and density of the resin and the average pore size in the bulk, the sound-absorbing characteristics of the foam can be tuned for best performance over a range of sound frequencies.