Your earnest money check gets made out to the Listing Broker, which is the company the listing agent works for. The earnest money check never gets made out to an agent. The check is deposited into the broker's account within a day or two. If the money is deposited and the deal didn't close because of attorney review, mortgage contingencies, etc., you and the seller would sign a "cancellation" and "release of earnest money" and that earnest money would be returned to you from the listing broker.
The earnest money will be credited to you on your closing statement (HUD) and is taken out of the funds you need to bring to closing. Besides closing costs and tax credits you also have a certain amount of money as your deposit ...the money you are putting down towards your loan. However, if the money you deposited was an overage...I've seen people bring too much to closing, the title company will issue you a check which is not the same as getting cash back at closing.
The check is made out to the listing agency, so that they may hold that money (typically in an interest bearing account if there's enough money), against the possibility that you might decide to walk away from the deal.
That check will be cashed, as soon as you've reached an agreement (signed and delivered) on your contract. If you've got more earnest money, than expenses at closing, any overage would be returned to you. This is NOT the same as the buyer getting "money back" at closing. This would just be the return of an overpayment.