How the Section 121 exclusion works
The exclusion isn't automatic just because you sold your home -- it depends on ownership and residence tests generally measured over five years, plus a separate two-year prior-exclusion restriction:
Ownership test: you owned the home for at least 2 of those 5 years.
Use test: you lived in the home as your primary residence for at least 2 of those 5 years (the 2 years don't need to be continuous, and don't need to be the same 2 years as the ownership test, though in practice they usually overlap).
Frequency test: you haven't excluded gain from a different home sale within the 2 years before this one.
Worked example: gain fully covered by the exclusion
You bought a home for $300,000, spent $25,000 on buying costs and a kitchen remodel, and sold it for $600,000 after $30,000 in selling costs. Net proceeds are $570,000; adjusted basis is $325,000; total gain is $245,000. As a single filer who owned and lived there for the full 5 years, your exclusion cap is $250,000 -- which fully covers the $245,000 gain. Estimated federal tax: $0.
Worked example: gain exceeding the exclusion
Same numbers, but you sell for $900,000 instead. Total gain is now $545,000. The $250,000 exclusion still applies, leaving $295,000 taxable -- taxed at long-term capital gains rates (and NIIT, if your income is high enough), the same way a stock sale would be.
Worked example: selling early for a qualifying reason
Suppose a new workplace is 65 miles from the sold home and the previous workplace was 15 miles away. The increase is 50 miles. If work is the main reason for selling, the other applicable conditions are met, and the shortest qualifying period is 12 months, a single filer could have a $125,000 partial limit: $250,000 × 12 ÷ 24. Confirm the full Publication 523 tests rather than using the distance between the old and new homes.
A loss isn't deductible
If your home sells for less than your adjusted basis, that loss can't be deducted against your other income or other capital gains -- the IRS treats a personal residence as personal-use property, and losses on personal-use property generally aren't deductible (unlike a loss on stock or a rental property). This is different from how capital losses on investments work, and it surprises a lot of sellers.