Housing is housing. Depending on where you live, when you buy, and what you pay, it can be a good investment or not. Everyone needs a place to live, and whether renting is better than buying, and how much you spend is totally market-specific. In NYC or SoCal I'd buy the most house I could afford, not in Missouri where I might take a different approach.
If you are taking a 30-year mortgage you're pretty leveraged; so housing can be a very awesome investment considering your return on invested capital. With stocks you are paying all the cash up front to get your [projected and speculative] return. With a mortgage, you only invest a small amount of your cash in the beginning and get the benefit of 100% of the asset appreciation in real time whether you've paid for it or not.
A 30-year mortgage carries a slightly higher interest rate, but it does offer the flexibility to pay it off anytime before 30 years anyway. With a 15-year mortgage, it's a way to build equity faster, pay off your mortgage sooner and save the interest expense. Many people like to get their house paid off before ER. Today, the S&P might be yielding returns that are worth it. Tomorrow, who knows?
You may want to get a 30-year mortgage with 13 payment periods per year as a compromise. It only costs you one extra payment per year, but can save years of payments over a 30-year term.