Generally, if you owned and used the home as your main home for periods totaling at least two years within five years ending on the date of these sale, you're eligible for the exclusion," says RIA's Trinz.
That's my reading of the rules, too. It's called the exclusion rule; basically, you can exclude up to $250,000 gain on the sale of your home, if you meet the following criteria:
During the 5-year period ending on the date of the sale, you must have:
1) Owned the home for at least 2 years (the ownership test), and
2) Lived in the home as your main home for at least 2 years (the use test).
The 2 years don't have to be consecutive, by the way. If you meet the exclusion requirements, you don't even have to report the sale on your taxes, at all.
If you don't meet the requirements, then you'll have to report the sale. But, you'll only have to pay tax on it if it resulted in a gain. Take the sale price, less any sales expenses (this is called the amount realized) less the adjusted basis. The adjusted basis is your original purchase price, plus the cost of any improvements done to the home. Doing all that arithmetic gives you a gain or loss. If it's a gain, you'll pay tax on it, but if it's a loss you'll actually owe less tax.
The relevant IRS publications are Publication 523 "Selling Your Home" and Publication 530 "Tax Information for First-Time Homeowners".