Earnest money is a deposit you put down when your purchase offer is accepted, signaling to the seller that you're serious. It's typically 1–3% of the purchase price, held in escrow by the title company or seller's broker until closing.
At closing, earnest money is applied to your down payment or closing costs. If the deal closes normally, you get full credit for it. If you back out for a reason covered by a contingency (financing, inspection, appraisal), you usually get it back.
If you back out for a reason NOT covered by a contingency, you can lose the earnest money. Read your contract carefully and never waive contingencies unless you're prepared to forfeit the deposit.