Shampoo makes bubbles because it contains chemicals called surfactants. These are molecules that are part hydrophobic (water-hating), and part hydrophilic (water-loving). They help to pull oils from your hair because the water-loving ends all try to point towards the water, while the water-hating ends want to point towards the oils. This suspends tiny drops of oil in the water, which you then wash out along with the shampoo.
However, surfactants have a second property that helps bubbles to form: they lower the surface tension of water. As you rub the shampoo in, water forms a film between strands, and then curves around the air to make a bubble. Your hair acts like a big collection of bubble wands to create lots of tiny bubbles, or foam.