When you list your property with a real estate agent, it will contain a term which describes who gets the earnest money in the event of buyer default. The purpose of an earnest money deposit is to provide assurances to the seller that the buyer has a legitimate interest in the property. When a buyer submits a purchase agreement, it will state the conditions under which the buyer will lose the right to his/her earnest money deposit. If the buyer defaults he/she forfeits their earnest money deposit.
Once there is a forfeit, you look at your contract with the real estate agent to determine who gets the earnest money. In my opinion, the seller should get the earnest money because s/he is carrying the property and incurs expenses while waiting for the deal to close. If the deal falls through, the agent can still collect his/her commission on a second sale. Moreover, if the sale falls through because the agent did not do his job (for example the agent did not properly review an "all cash" buyer and found out that the buyer does not have the funds to close the deal), it seems unfair that the agent should share in the earnest money.
I actually think that allowing an agent to get any of the earnest money is a conflict of interest for the agent. They can make money by "tanking" the deals. I had a situation where the agent got 50% of the earnest money on a million dollar listing. They had two sellers walk away because of default. The agent then sold the house to the third buyer. The the agent walked away with their full commission and earnest money payment that was MORE than their commission. Meanwhile, the owner of the house carried the house for over a year waiting for a sale and seeing sales fall through for reasons relating to the shortcoming of the agent and the agent's review of the qualifications of the buyers.
I would recommend negotiating that the seller gets 100% of the earnest money. Alternatively, the agent should get no more than what they would get if they sold the property, typically 3% or 6%. Sales people do not get paid for failing to perform, neither should real estate sales people.