Why Polystyrene is foamy?
Not all polystyrene are foamy. The ones that are foamy have been expanded by air or have trapped air in its mold.
Here is polystyrene pellets before and after expansion
Here is another example of polystyrene that is not foamy
The ones that are foamy are either expanded polystyrene (EPS) and extruded polystyrene foam (styrofoam). As can be seen from picture below, people always confuse between the two.
Expanded polystyrene is expanded inside steam chamber which involves air entering the polustyrene pellets and inflate them (and at the same time pentane is removed).
Extruded polystyrene foam is liquified and extruded through a die. In this process, air is trapped inside the foam.