I'd say the mortgage example is special, as it's not just an investment for investment's sake, it's a house you are actually going to use, and your realistic alternative is to rent a place on indefinite terms.
For most folks, investments that beat the loan interest is a tricky and often outright risky proposition. If you are leveraging yourself too far then you are almost certainly not getting a good rate, and the investment may be assumed to do better, but could go significantly worse.