Source: Phillip Ingham via Flickr.

You may not realize it, but you're not always free to keep all the gains you've earned from an investment. In fact, thanks to a little levy known as the capital gains tax, Uncle Sam may take a big share of the money you've made by investing wisely and assuming risk. So what is the capital gains tax, and how does it work?

The capital gains tax is a charge levied by the government when you sell an investable asset (like a stock or a bond) for more than you paid to buy it. In addition to traditional investments, the capital gains tax gets levied on collectibles and precious metals, and you may even find yourself paying it when you sell your home.

The bad news is that capital gains taxes can be somewhat complicated. The good news, though, is that capital gains are often taxed at less than your ordinary income tax rate, and you can often use capital losses to offset capital gains, potentially reducing the tax bite even further.

What are the capital gains tax rates?
Capital gains are generally considered either "short term" or "long term," depending on how long you have held the asset you're selling. You generally have to hold an asset for at least a year and a day for it to be considered a long-term gain. If you've sold an asset short (sold it first and then bought it back later), it's taxed as a short-term gain regardless of how long you held the short position open.

Short-term capital gains are taxed as if they were ordinary income -- at your marginal income tax rate. Long-term capital gains are generally taxed at a lower rate -- or at least at a rate no higher than your marginal income tax rate. The chart below comes from the Internal Revenue Service and shows the capital gains tax rates:

Source: U.S. Internal Revenue Service, publication 550. 

Further, your capital gains may be subject to an additional "net investment income tax" of 3.8%, depending on your income and tax filing status. The table below comes from the IRS and shows the modified adjusted gross income thresholds that would make your capital gains also subject to that net investment income tax:

Source: U.S. Internal Revenue Service, NIIT Frequently Asked Questions page 

Your home is a special asset when it comes to the capital gains tax. If you meet certain tests of ownership and occupancy, you can exclude up to $250,000 (or $500,000 if married filing jointly) of gains on the sale of your primary home from your income.

To qualify for that exclusion, during the five year prior to the sale, you must have owned the home for at least two years (the ownership test) and lived in the house as your primary residence for at least two years (the use test). And during the two-year period ending on the date of the sale, you must not have excluded the gain from the sale of another home.

Offsetting gains with losses
Within a given tax year, your capital losses can be used to offset your capital gains. If your capital losses exceed your capital gains, you can use up to $3,000 of those losses ($1,500 if married filing separately) to offset ordinary income. Any additional capital losses are "carried forward" to future years and can still be used to offset future gains. 

Short-term losses first offset short-term gains, and long-term losses first offset long-term gains. If, after that like-for-like netting, you still have losses of one type left over, you can use those losses to offset the other type of gain. Only after that point would you use your capital losses to partially offset your ordinary income or carry them forward for future years.

Complicated but worth understanding
The capital gains tax is complicated, but it's certainly worth getting to know if you're an investor. After all, that tax only applies if you close an investment. While you may sometimes be forced out of an investment because, for example, it was acquired or a fund you own sold its position, you generally have significant control over whether and when to take a capital gain.

It's a good rule not to let the tax tail wag the investment dog, but it's also important to consider the net amount you'll be left with after taxes when you close out your investments. Successful investing can create an incredible source of wealth, and once you understand exactly what is the capital gains tax, you can capture more of the wealth you generate from your investing for yourself.