Image source: Getty Images.

The year is rapidly coming to a close, and if you've sold any investments at a profit this year, you should anticipate paying capital gains taxes when you file your tax return -- that is, unless you take advantage of tax-loss harvesting, which is the practice of selling losing investments in order to offset your profits. Here's how it works, and what you need to know.

What is tax-loss harvesting?

Tax-loss harvesting, also known as tax-loss selling, is the practice of selling losing investments with the specific intention of claiming a capital loss on your taxes. It's generally done toward the end of the year, but it can be done any time.

The IRS allows investment losses to offset capital gains taxes you may owe from selling investments at a profit. For example, if you sell a stock at a $3,000 profiit and then sell another stock at a $2,000 loss, your capital gains for tax purposes will be just $1,000 for the year.

The rules

Before you start selling your losing investments, here are some things you need to know.

1. The "wash sale" rule: You can't claim a loss for tax purposes if you sell an investment and then buy the same (or a substantially identical) investment within 30 days. For example, if I sell shares of an S&P 500 mutual fund at a loss, I can't buy more of the same mutual fund or another mutual fund that tracks the S&P 500 within 30 days, if I plan on using the loss on my tax return.

2. Losses are applied to the same type of capital gains first -- long/short term. In other words, if you sell a stock that you've held for five years at a loss, it will be used to offset long-term capital gains first, before it can be applied to short-term gains.

3. If your deductible losses exceed your capital gains, up to $3,000 of losses can be used to reduce your other taxable income. Any excess can be carried over to the next year.

How much could you save?

Let's say you're in the 25% tax bracket, which translates to a long-term capital gains tax rate of 15%. Earlier in 2016, you sold a stock you've owned for eight years at a total profit of $10,000, which should result in a capital gains tax of $1,500. However, you decide to finally sell a stock you've been hanging on to for several years, at a $4,000 loss. This reduces your taxable long-term capital gains to $6,000 for the year, resulting in a capital gains tax of $900, a $600 savings.

Take a look at some of the losing investments you've been holding on to. (If you don't have any -- congrats!) How much could you save? Here's a link to the current U.S. tax brackets – if you're in the 10% or 15% bracket, your long-term capital gains tax rate is 0%. If you're in the top tax bracket, your rate is 20% and you may have to pay an additional 3.8% if your income is over a certain threshold. All other tax brackets have a 15% long-term capital gains rate.

Short-term capital gains tax rates are the same as your tax bracket and are applied to sales of investments held for one year or less.

Just to be clear ...

To be perfectly clear, if you're holding some losing investments but still believe in their long-term potential, don't sell them just to reap the tax benefits. For example, in my own portfolio I'm sitting on a pretty significant loss in Fitbit (NYSE: FIT), however I still have a positive long-term outlook for the company, so I won't be selling anytime soon, even though doing so would result in tax savings.

On the other hand, if an investment just hasn't worked out and you feel you'd be better off deploying that capital elsewhere, you may want to consider doing some tax-loss selling before the end of the year.