We've all made an investing mistake, and suffered the consequences. Luckily, Uncle Sam makes taking stock losses a little easier by giving investors the opportunity to write off losses at tax time.

Under the tax code, investors can write off any amount of losses against their gains. Thus, if you lose $50,000 on one stock and make $50,000 on another, these gains and losses will offset each other. You won't owe any taxes on your $50,000 in gains because of your equally sized losses.

If your losses exceed your gains, you can write off up to $3,000 of the excess losses each year against your income. Thus, suppose you lose $53,000 on one stock and gain $50,000 on another. The gains and losses cancel out up to $50,000. The remaining $3,000 can be written off against your ordinary income during the year.

If your losses exceed your gains by more than $3,000, you'll have to carry your losses forward to future tax years. Thus, it's possible that if you take a very large tax loss in one year, you'll be able to write off a portion of your losses for years and years to come.

Maximizing your tax losses
The tax code is written such that short- and long-term capital gains and losses must first offset losses of the same type. Thus, short-term losses should offset short-term gains, and long-term losses would offset long-term gains.

However, if your losses from one type exceed the gains of the same kind, you can apply the excess to another type of gain. Thus, if you only had a short-term gain of $5,000 and a short-term loss of $10,000, you could apply the extra $5,000 of short-term losses to long-term gains.

Ideally, you'd want to match long-term losses with short-term gains. Short-term gains are taxed at the highest rate under the tax code, because short-term capital gains are treated as ordinary income and taxed at your marginal tax rate. Financial advisors and accountants can help a lot here. Proper tax planning suggests you should seek to minimize or offset short-term capital gains whenever possible because short-term gains are taxed at the highest rate.

Of course, the best way to avoid all this trouble is to make investments in a tax-deferred account like a 401(k) or Individual Retirement Account (IRA). In doing so, you'll be able to buy and sell freely without consideration for differences in taxation.

Save the space in your tax-deferred accounts for investments that generate a lot of taxable gains or losses, and put the most passive investments in a taxable account. Capital gains are the United States' only voluntary tax. You decide when to pay taxes by deciding when you sell an investment to lock in a gain.

This article is part of The Motley Fool's Knowledge Center, which was created based on the collected wisdom of a fantastic community of investors based in the Foolsaurus . Pop on over there to learn more about our Wiki and how you can be involved in helping the world invest, better! If you see any issues with this page, please email us at[email protected] . Thanks -- and Fool on!