In most cases of purchases the general advice is to save the money and
then make the purchase. Paying cash for a car is recommended over
paying credit for example. For a house, getting a mortgage is
Says who? These rules of thumb hide the actual equations behind them; they should be understood as heuristics, not as the word of god.
The basic idea is, if you pay for something upfront, you pay some fixed cost, call it X, where as with a loan you need to pay interest payments on X, say %I, as well as at least fixed payments P at timeframe T, resulting in some long term payment IX.
To some, this obviously means upfront payments are better than interest payments, as by the time the loan is paid off, you will have paid more than X. This is a good rule of thumb (like Newtonian's equations) at low X, high %I, and moderate T, because all of that serves to make the end result IX > X.
Are there circumstances where the opposite is true? Here's a simple but contrived one: you don't pay the full timeframe. Suppose you die, declare bankruptcy, move to another country, or any other event that reduces T in such a way that XI is less than X. This actually is a big concern for older debtors or those who contract terminal illnesses, as you can't squeeze those payments out of the dead. This is basically manipulating the whole concept.
Let's try a less contrived example: suppose you can get a return higher than %I. I can currently get a loan at around %3 due to good credit, but index funds in the long run tend to pay %4-%5. Taking a loan and investing it may pay off, and would be better than waiting to have the money, even in some less than ideal markets. This is basically manipulating T to deal with IX.
Even less contrived and very real world, suppose you know your cash flow will increase soon; a promotion, an inheritance, a good market return. It may be better to take the loan now, enjoy whatever product you get until that cash flows in, then pay it all off at once; the enjoyment of the product will make the slight additional interest worth it. This isn't so much manipulating any part of the equation, it's just you have different goals than the loan.
Home Loan Analysis
For long term mortgages, X is high, usually higher than a few years pay; it would be a large burden to save that money for most people.
%I is also typically fairly low; P is directly related to %I, and the bank can't afford to raise payments too much, or people will rent instead, meaning P needs to be affordable. This does not apply in very expensive areas, which is why cities are often mostly renters.
T is also extremely long; usually mortgages are for 15 or 30 years, though 10 year options are available. Even with these shorter terms, it's basically the longest term loan a human will ever take. This long term means there is plenty of time for the market to have a fluctuation and raise the investments current price above the remainder of the loan and interest accrued, allowing you to sell at a profit.
As well, consider the opportunity cost; while saving money for a home, you still need a place to live. This additional cost is comparable to mortgage payments, meaning X has a hidden constant; the cost of renting. Often X + R > IX, making taking a loan a better choice than saving up.
"The general advice" is a good heuristic for most common human payments; we have relatively long life spans compared to most common payments, and the opportunity cost of not having most goods is relatively low.
However, certain things have a high opportunity cost; if you can't talk to HR, you can't apply for jobs (phone), if you can't get to work, you can't eat (car), and if you have no where to live, it's hard to keep a job (house). For things with high opportunity costs, the interest payments are more than worth it.