Earnest money is a deposit that shows the seller you're serious. The amount is negotiated, it's held by the title company, and it goes toward your costs at closing.
You generally get it back if you cancel within your inspection window or the contract ends under one of its terms, but not if you simply walk away later.