Earnest money is a deposit put down by the buyer to show the seller that he is serious about buying the house. If the seller accepts the offer, the earnest money is paid to the title company in escrow. How much is required will depend on the state's real estate transaction policies, the current real estate environment, and what the seller wants. On the average buyer's market, some sellers require less than 1 percent. In a seller's market, the deposit required can be 2 to 3 percent. .
If the sale goes through, the earnest money can go toward funding the down payment. Then buying process proceeds, including the home inspection. If something wrong is found with the house and the buyer no longer wants to buy, he can usually get most of his deposit back. If the sale does not go through for any other reason, the buyer will need to abide by what the purchase agreement says about refund of deposits. 
Earnest money protects the seller from frivolous buyers who do not commit to their offers. Once an offer is made and accepted, the house is taken off the market. If the buyer simply changes his mind and decides not to buy without just cause, the seller has to go through the trouble of putting the house back on the market. Not to mention, the seller lost valuable time. The deposit is a way of compensating the seller for the inconvenience. 
Here's a little video explaining what earnest money is.