Phone Subsidies

Can someone explain exactly how the carrier gets their money back on subsidized phones? I understand subsidized is usually worse, and I always buy my phones unlocked anyway, but I got in an argument with someone telling me I overpay for devices and that their on contract SGS4 was way cheaper and I got ripped off. I repeated to tell them that the carrier does indeed recoup their money and that they are pretty blind for not knowing that but I don't know the exact math behind it.

TLDR: How do carriers make money back from subsidies?