Buying quality stocks and holding them for a long time is an investing strategy that could really pay off in the long run. But sometimes, it pays to sell a stock sooner, either because its value has climbed and you don't expect it to stay that way for long, or because its value has consistently decreased and you don't expect it to recover.

Selling stocks can be a strategic move, but there are tax implications involved. Here's what you need to know.

Selling a winning stock

When you sell a stock at a price that's higher than what you paid for it, you'll be subject to capital gains taxes on that sale. But the amount of tax you'll pay will hinge on how long you held that stock before selling it.

Man sitting at a laptop taking notes on a pad of paper

Image source: Getty Images.

Stocks that are held for a year or less are subject to short-term capital gains taxes, which mimic the marginal tax rates that apply to ordinary income. Meanwhile, stocks that are held for at least a year and a day before being sold are subject to long-term capital gains taxes, which come in at a much more favorable rate. Long-term capital gains taxes amount to 0% for lower earners, 15% for moderate to high earners, and 20% for the ultra-wealthy. By contract, marginal tax rates top out at 37% for extremely high earners.

To highlight the difference, let's say you're single and report $100,000 a year in income. Your marginal tax rate will be 24%, which means if you sell a stock you've held for a year or less that results in $1,000 in gains, you'll pay $240 in taxes.

Now, let's say you held that same stock for at least a year and a day before selling it. In that case, you'll only pay 15%, or $150, in taxes, because that's the capital gains tax rate you'll be subject to.

Selling a losing stock

If you sell a stock for less than what you paid for it, you won't owe any taxes on that sale at all. In fact, you'll be able to use that sale to cancel out other capital gains for the year.

Say you take a $2,000 loss on the sale of some stock, but also sell another stock that results in a $2,000 gain. Your loss will wipe out your gain so you won't owe the IRS money on it. Furthermore, if your loss exceeds your capital gains, you can apply the remainder to up to $3,000 of ordinary income so the IRS doesn't tax you on that portion of your earnings.

Know what taxes you'll pay

Understanding how investment gains are taxed can help you make smart decisions that minimize your IRS burden. Say you're getting close to the one-year mark and are looking to sell a stock that's up. Waiting just a few more days could spell the difference between paying short-term capital gains taxes versus long-term capital gains taxes, so always pay attention to when you first added your various stocks to your portfolio and time the sale of your stocks accordingly.