This is a bet on the future price of the home. And a lot of simulations have shown that you will come ahead in stocks unless you are in an exceptional growing market and not moving for at least 5 years.
There is a huge lobby in society to convince you that owning a home is the way to go (banks, real estate agent, other homeowners and people repeating this nonsense all the time). Don't let this fool you and make the calculations before buying anything.