What I found is that on a time horizon of ~7 years, it absolutely makes sense to buy a home. The reason is that when you have a mortgage, you're making a highly leveraged investment. If you buy a house for 100k and put 20k down, then you're 5x leveraged. If you're able to sell the house for 110k. The price has increased 10%, but your ROI is 50%.
There was a definite inflection point after 7 years, however, where the amount of leverage decreases to the point that the higher gains in the market begin to dominate the modest increase in home value.