When you buy any kind of asset and later sell it for profit, that's called capital gains. Unfortunately, the federal government likes to share in your good fortune by requiring you to pay a tax on said capital gains. The good news is that there's a way to reduce, or even entirely avoid, this tax.

How capital gains taxes work

Like most taxes, the capital gains tax gets a bit complicated when you look at it more closely. First of all, there are different capital gains tax rates depending on how long you've owned the asset in question. If you've owned it for one year or less, you fall under the category of short-term capital gains and are required to pay the short-term capital-gains tax, where the tax rate is the same as your top income-tax bracket. In other words, if you fall into the 25% tax bracket, your short-term capital gains tax is also 25%.

Holding an asset for more than one year before selling it turns it into a long-term capital gain, which is taxed at a discounted rate -- usually no more than 15%. Taxpayers in the top tax bracket pay 20% long-term capital gains taxes, while those in the 10% or 15% income-tax brackets pay 0% on long-term capital gains.

Certain types of assets, such as art and collectibles, are charged long-term capital gains taxes at a higher rate of 28%. The tax-reform package that's currently being debated in Congress repeals this 28% rate, but unless the bill passes and becomes law, we're stuck with it.

Taxes spelled out in colored blocks.

Image source: Getty images.

Capital losses erase capital gains

If, when you sell an asset, you lose money, you have a capital loss rather than a capital gain. Losing money never feels good, of course, but there's a nice upside -- any capital losses you suffer during the year are subtracted from your capital gains for tax purposes.

For example, say you sold two different kinds of stock during the year. On the first sale, you made a $4,000 profit, and on the second sale, you lost $3,000. The $3,000 loss would be subtracted from the $4,000 gain, and you'd only be required to pay capital gains taxes on $1,000 worth of gains.

Tax-loss harvesting

The strategy of planning out your asset sales to take advantage of this "capital gain minus capital loss" equation is called tax-loss harvesting. It's a powerful way to reduce taxes that few investors use because of the psychological pain of selling something at a loss.

If you buy an investment and that investment drops in value, you can tell yourself that it will rebound at some point in the future -- as long as you still hold the investment. If you sell it and make the loss real, it's tantamount to admitting that you made a mistake -- something that many people prefer to avoid. However, letting this fear drive your investment decisions will cause you to lose money in another way: You'll end up paying all your capital-gains taxes instead of reducing them by dumping your lost-cause investments.

Psychological jujitsu

Actually, if you look at it a certain way, you'll realize that selling an investment at a loss -- when timed correctly -- turns the loss into a partial gain, and makes it not such a bad mistake after all. Consider the above example of a $4,000 gain countered by a $3,000 loss. Assuming that it's a long-term capital gain and you're in the 25% tax bracket, a $4,000 gain would normally generate $600 in capital gains taxes.

Because you used your loss to cancel out most of this gain, instead you'll only have to pay $150 in capital-gains taxes. That means you'll save $450 on taxes. So by timing the sale correctly, you didn't actually lose $3,000, you only lost $2,550.

That still stings, of course, but at least you managed to turn a potential disaster into a tax advantage. And if your transaction had been a short-term capital gain instead of a long-term one, you'd have saved even more in taxes thanks to tax-loss harvesting.