Say I'm squirreling away $5k a year for a new car in 4 years. Every year I put in another $5k, so by the end, I've put in $25k.
If I put that in a typical savings account, I earn 0.1% and come out $50 ahead. Effectively 0, or losing value once you throw in inflation.
Go with stocks, at a 5% return, I end up with over $27k, and at 8% I have over $29k. It's also true that I could lose value, and that's the gamble.
So it's a matter of your comfort level, obviously, and if you can't afford to lose the money, don't invest. But in many many cases, the reward outweighs the risk.
I view it that I'm much, much more likely to get a return > 0.1% than experience a loss over 5 years, and I'm willing to accept the risk.