IMO: When you do it for the entertainment/feeling/rush, it's gambling. When you do it for the returns, it is investing. I also think the other poster that mentioned investing as being interested in the success of the endeavor, that would exclude shorting and I think might be a useful distinction.
Casino games and sports betting all have lower expected value (probabilistic value) than their cost, so they are not something you can do for returns (you have better expected returns by not participating).
There are plenty of people that are misinformed, dishonest, or stuck finding a bigger fool that will sell you a gamble by calling it an investment, and expected value is not guaranteed value.
Just because you are wrong about your expected value calculations (or were right but the actual return was on the lower end of the range) and have made a bad investment doesn't change the fact that it was an investment because you were doing it for the returns.
In short, performance doesn't matter for this distinction, at least IMO.
The DJIA (e.g.) isn't "the house". It isn't something you are competing with in that your losses are its/their gain. You are misunderstanding both investing (in general and the stock market specifically) and gambling when you make that confusion/analogy.
Not beating the market but having positive returns is only "losing" when infinite exponential growth is the goal. Beating the market but having negative returns is not "winning".