It's a great feeling when you sell your house for a substantial profit. You can get rid of your remaining mortgage balance and walk away with a nice wad of cash toward your next home purchase. However, it's not such a great feeling when you have to hand over a chunk of that newfound cash to the IRS in the form of capital-gains taxes. That's why it's so important to understand -- and take advantage of -- the home sale tax exclusion.

The home sale exclusion is a tax break provided by Congress to encourage homeownership. Meet certain requirements set by the IRS, and you can exempt up to $500,000 of your gain on the sale from taxes. Mind you, that's $500,000 of the gain, i.e., the profit. The portion of the selling price that's equal to the money you've invested in the house is already not subject to taxes, so you don't need to worry about excluding it.

Shaking hands over a contract and a miniature house

Image source: Getty Images.

Home sale exclusion requirements

The full exclusion amount is $500,000 for married taxpayers filing jointly and $250,000 for everyone else. In order to qualify for it, you have to pass the eligibility test --though if you don't meet all the criteria, you may still be able to claim a partial exclusion of your capital gains. The primary requirements are that the house you're selling must have been your main home for at least two out of the past five years, and that you haven't claimed the home sale exclusion already during the past two years.

If you only own one house, then you can probably hazard a guess as to whether it qualifies as your "main home" (hint: It does). However, if you own and live in more than one house, then it can be trickier to decide which one is your main home. Usually, the IRS will say that the house you live in the majority of the time is your main home. However, if you use the home's address as your mailing address on important documents (such as your tax return) and it's near your workplace and other places you visit regularly (such as friends and family), you may be able to claim it as your main home even if you don't spend the majority of your time there.

Qualifying for a partial exclusion

There are a few workarounds for these requirements that may allow you to claim at least a partial exclusion. If you had to move before you'd spent the minimum two years in the house because of work or health reasons, you can get a prorated exclusion based on how long you did live in the house. For example, if you'd lived there only one year and were then forced to move because you were transferred to a new office that's at least 50 miles further away from your house than the old office was, then you could claim a 50% exclusion. In that case, you'd get to exclude $250,000 of gains if married filing jointly or $125,000 of gains if filing under another tax status. You can also claim a partial exclusion if you were forced to move because of health issues or because you had to leave to care for an ailing relative.

How to figure out your gain

Of course, before you can claim the exclusion, you need to know just how much of a gain you made on your home sale. First, add up the amount you received for selling the house -- this includes the value of any debt that the buyer is taking over and any real estate taxes they agreed to pay for you. Then subtract the expenses you incurred during the selling process. This includes any commissions you paid to real estate agents, fees for advertising the house, legal fees, and so on. The result is the amount you realized on the sale, to put it in legal language.

Next, you need to figure out your "basis" in the house, meaning how much money you've invested in it up to this point. Your basis includes the amount you originally paid for the house, plus the cost of any home improvements you've made that increased the house's value. If you had to repair any damage to the house or land, such as damage caused by a flood or fire, then you can add in the cost of the repairs minus any reimbursement you received from insurance companies or other sources.

Finally, subtract your basis from the amount you realized on the sale. The result is your gain on the sale. If it's equal to or less than your home sale exclusion, then you won't have to pay any capital gains taxes at all on the transaction. If it's greater than the home sale exclusion, you'll have to pay capital gains taxes on the excess. For example, lets say you're single and you qualify for the full home sale exclusion. If your gain on the sale of your home was $300,000, then you can exclude $250,000 for tax purposes, and you'll only have to pay capital gains tax on the remaining $50,000.

Reporting your home sale to the IRS

Whether or not you qualify to exclude all of your gain from taxes, you need to tell the IRS about the sale. If you don't, you may end up facing an IRS auditor who believes you are hiding taxable gains. You can report your home sale to the IRS by filling out Form 8949 and using the information from this form to fill out a Schedule D, which will accompany your Form 1040 when you send in your tax return for the year. If you have any questions about how to fill out either of these forms, consult with a tax professional -- making a mistake on these documents could be very expensive.