Some other answers mention the ability to sell at grant. This is very important. If you have that ability, think about your guaranteed return.
In my case, I get a 15% discount on the lowest 6 month window price from the last two years.
If you do the math, the worst case return can be calculated:
1) Money that from the beginning of the window, I make 15% for 6 months (30% annual return guaranteed)
2) Money at the end of the window (say the last month) is 15% for one month (180% annual return guaranteed)
In the end, your average holding window for your money is about 3 months (you can calculate it exactly). At that rate, you have a guaranteed 60% annual return. You can't beat that anywhere, with a significant upside if your company stock is increasing. So, if your company has an instant sell at grant option, you have to be brain dead not to do it. If it takes time to get your shares, then you need to look at the volatility of the stock to see how big the chance of losing money is.
To generalize to a formula (if that's what you want):
WM = purchase window (in months);
D = Discount Percentage;
GR = Guaranteed Return
GR = 12/(WM/2) * D = 6*D/WM
One last thing, If you are going to participate in ESPP, make you that you understand how to do your taxes yourself. I haven't found a tax person yet who does ESPP correctly (including an ex IRS agent), so I always have to do my taxes myself to make sure they get done correctly.