Do you get earnest money back if the inspection fails?
Usually, yes, if your contract has an inspection contingency and you cancel in writing before it expires. Homes do not technically pass or fail an inspection. The contingency lets you back out if you do not like what the inspector finds, and get your earnest money back.
The catch is the deadline. Inspection periods are often short, and once yours ends without a written cancellation, the protection is gone. Some contracts work differently. In Texas, for example, many buyers pay a separate option fee for the right to cancel during an option period.
If you waived the inspection contingency to make your offer stronger, a bad report alone may not be a reason the contract recognizes, and the seller could claim the deposit. Read your contract's exact terms, or see the glossary entry on the inspection contingency.