Investing is a great way to grow your wealth. But the money you earn on investments isn't always yours to keep. Just as the IRS is entitled to a portion of the income you generate from your job, it can also come after the income you earn in your investment portfolio. And that can hurt in a number of ways.

First, paying taxes just plain stinks. But also, many people don't pay estimated taxes on their investment gains during the year, and as such, they wind up owing money when they file their tax returns. If you land in this category and don't have the cash to pay your tax bill in full, you'll risk racking up penalties and interest. Ouch!

A better bet: Aim to keep the IRS as far away from your investment earnings as possible. Here's how.

Man counting hundred-dollar bills

Image source: Getty Images.

1. Hold investments for at least a year and a day

Anytime you sell investments at a price that's higher than what you paid for them, you're liable for capital gains taxes. The length of time you hold investments before selling them, however, will dictate what tax rate you'll be subject to.

If you hold investments for a year or less and sell them at a profit, you'll be hit with short-term capital gains, which are taxed the same way ordinary income is taxed. On the other hand, if you hold investments for at least a year and a day, you'll land in the more tax-friendly long-term capital gains category. The tax rate for long-term capital gains tops out at 20% for higher earners, but most people pay 0% or 15%. Therefore, keeping investments in your portfolio for the right amount of time could pay off in a very big way.

2. Load up on municipal bonds

The great thing about buying bonds is getting to collect interest that can serve as a steady stream of income, or cash to reinvest. The problem, however, is that the IRS is entitled to a share of that interest -- that is, unless you buy municipal bonds.

Municipal bonds are those issued by states, cities, and counties (as opposed to corporate bonds, which, as the name implies, are issued by corporations). The great thing about municipal bonds is that their interest is always exempt from taxes at the federal level. And if you buy municipal bonds issued by your home state, their interest is exempt from federal, state, and local taxes.

Keep in mind that if you sell municipal bonds for a price that's higher than what you paid for them, you'll still be on the hook for taxes on that gain. It's just the interest generated by those bonds that you won't need to pay taxes on.

3. Sell losing investments to offset gains

Though gains on investments are taxable, losses on investments have the opposite effect -- they can reduce your tax burden. Investment losses can be used to offset capital gains so that if you take a $2,000 loss but have a $2,000 gain, you get to break even rather than owe the IRS anything. Furthermore, if your investment losses for the year exceed your gains, you can use them to offset up to $3,000 in taxable income.

What happens once your losses cancel out your gains as well as cover $3,000 of income? You get to carry the remainder of your loss to the next tax year. Therefore, if you have poorly performing investments in your portfolio, you may be better off liquidating them rather than retaining them in the hope that they'll come back up in value.

Nobody wants to pay more money to the IRS than necessary. Be smart about the way you manage your investments, and with any luck, you'll get to keep more of your money for yourself.