Answer:
Generally speaking, it has baffles inside that trap the gases. See the associated link for diagrams.
ADDING: Oh, and silencers are not really "silent" either. Most of them make a powerful gun sound like a cap pistol. Some of them make a handgun or .22 sound like a CO2 powered pellet gun. I own a silencer and I have personally shot several others and witnessed several others on various kinds of guns being used right next to me at shooting ranges.