It cleans out dirt, but also the oil that makes your hair shiny and strong. However shampoo's sold as something your hair really needs, and the truth is suppressed. There are several common sorts of soapy things. They attract oils by electric charge, allowing the oil to be emulsified with the water. For some reason, these cleaners are themselves made from fats.
Soap is made from animal or vegetable oil, and detergent is made from mineral oil.
Detergents can be made more powerful. Shampoo is detergent, just like dishwashing detergent, bathroom cleaner, and engine block cleaner. Soap was better suited to washing hair, because it didn't remove so much of the oils that are naturally in hair. But the water supply slowly changed. It's now generally more alkaline, which people call hard water.