They buy mortgage-backed securities in the secondary markets which, in simple terms, are bonds issued by securitization agencies (well, mostly by Fannie Mae and Freddie Mac in the residential mortgage area) to fund pools that buy and collect individual residential mortgages. The higher prices for MBSes leads to cheaper funding for such pools.
If funding of mortgages is cheaper for banks and agencies, then they become cheaper for the borrowers as well. Even if you aren't buying a new home, you can refinance your existing mortgage at a lower rate.