And correct me if I'm wrong, but buying it at a 15% discount and then selling at 15% tax rate(isn't that what capital gains is) would pretty much do nothing, right?
No, that's not right.
Short version: ESPP discount is free money, like a 401k match. If you aren't limited in when you can sell it (like velocistar said, if you have to leave the company first) then put the max in that you can, then sell immediately so you don't have to worry about the complicated stuff.
Long version:
Step 1. You get paid: 401k is deducted pre-tax; then you pay taxes; then your ESPP is deducted from your net pay. (So you pay taxes on the ESPP contribution up front, at your normal tax rate).
Step 2. ESPP is used to buy stock at 15% discount, for example if you have $850 in ESPP and the stock is $100/share market price, you get 10 shares for your $850.
Step 3. When you sell the ESPP, depending on when you sell it and what the price is when you sell it, one of several situations occurs:
* If you sell in less than 1 year since the ESPP was used to buy the stock, then you pay taxes at your normal rate on the amount you gained from the discount at the time of the stock purchase, and then have a short-term capital gain/loss depending on where the stock price has moved since the purchase was made. This is considered a "disqualifying disposition," and your employer will probably include the amount you gained from the discount on your W-2.
Example: You sell the stock as soon as possible after the purchase, to lock in your "free money." Using the previous numbers, you'd get ~$1000 in proceeds from the sale. $850 of that is what you put in - you're already taxed on that as part of your wages, so it's not taxed again. $150 of that is gain from the discount - it should be added to your wages when you file taxes for the year that you made the sale, and it's taxed just like the $850. So, if your marginal tax rate on this money is 25%, then you would get $637.5 after taxes if you DON'T use the ESPP. If you do use the ESPP, then you get $750 after taxes - $637.5 from your contribution, plus $112.5 in free money from the discount.
Example: 6 months after the ESPP purchase, the stock price doubles and you sell. Using the previous numbers, you'd get $2000 in proceeds from the sale. $850 of that is what you put in - you're already taxed on that as part of your wages, so it's not taxed again. $150 of that is gain from the discount - it will be added to your wages in the year that you make the sale (which might be the year after you paid taxes on the original $850), so you'll pay regular taxes on that $150. The other $1000 is short term capital gain, which is also currently taxed at your normal rate.
Example: 6 months after the ESPP purchase, the stock price halves, and you sell. You'd get only $500 from the sale. You were already taxed on the $850 you put in. You'll still be taxed on the $150 gain from the discount. Then you have a $500 short term capital loss, which is kind of like a tax deduction, and will usually cancel out the extra taxes you paid on the income (since you were taxed on $1000 but only got $500 out of it).
*If you sell more than 1 year after the ESPP purchase, but less than 2 years after the "grant date" (for my employer, the ESPP buys stock every 3 months; the grant date is 3 months before the purchase date), then it's still a "disqualifying disposition." The only difference is that any capital gains are long-term, which means that the 15% tax rate (which is probably lower than your marginal tax rate on wages) applies.
Example: A year and a day after the ESPP purchase, the stock price has doubled and you sell. Proceeds are $2000. You already paid taxes on $850, so that has no additional tax. You pay your marginal tax rate on $150 gain from the discount. You pay 15% taxes on the $1000 long term capital gain. (This is a better deal than if you had sold at 1 day less than a year, because then you would have paid your 25% marginal rate on the capital gain).
*If you sell more than 2 years after the grant date, then funny things happen. It's a "qualifying disposition."
Example: 3 years after the ESPP purchase, the stock has doubled and you sell. Proceeds are $2000. You already paid taxes on $850, so that has no additional tax. At the grant date, (in my case 3 months before the purchase), the stock price was only $90 instead of $100 - instead of paying regular taxes on the actual discount of $150 (15% of the $1000 that the stock was worth when you bought it), you pay regular taxes on the theoretical discount - 15% of the $900 that the stock was worth at the grant date, or $135. Then, you pay long term capital gains on the $1000 of capital gain, plus the $15 extra that you didn't pay regular taxes on because it was a qualifying disposition. Since long term capital gain tax is usually lower than your regular tax, this is an improvement (although a very minor one).
Example: 3 years after the ESPP purchase, the stock has halved and you sell. Proceeds are $500. You already paid taxes on $850. If it were a disqualifying distribution, then you would still pay regular taxes on the $150 discount, but because it's a loss and it's a qualifying distribution, you don't pay those taxes. You also have a long term capital loss of $350 (the rest of the difference between the $1000 it was initially worth , which is similar to a tax deduction.
Confused yet? That's why I recommended just selling ASAP.