If the deal falls through, the fate of the earnest money depends on the circumstances and the terms for returning earnest money outlined in the sales contract.
Usually, you get your earnest money back when it's not your fault the deal fell through, like the home failing the inspection. But, on the other hand, if you back out for no reason, the seller may be able to keep the earnest money.
Again, the sales contract outlines the specific circumstances under which the seller may retain the earnest money. You should carefully review the purchase agreement with an attorney or an experienced real estate agent to understand your rights and obligations as a buyer before paying the earnest money.