The rent is the maximum you will ever pay for that period. The mortgage is the minimum you will pay and doesn't include the downpayment, repairs, HOA, Insurance and tax increases. It also doesn't include the closing costs, realtor costs, all the fees.
This is why the math is so complex. People easily forget all those fees and costs to make the math look simple.
This also takes into account that you invest into the US market and equity with all the cash that you would have had to spend on your house.
I'm talking about the US, but this holds true in multiple countries.
To answer your last question: In almost all HCOL (SF, Seattle, ...) you would have been way better renting than buying, except if you time it perfectly. If you look at today's market you would almost definitely be better renting than buying (But nobody knows the future...)