If the latter is the case, the check will be returned to you promptly. If your negotiations are successful and the Residential Contract of Sale is ratified, your earnest money deposit has to be put into an escrow account within 7 business days of ratification.
More often than not, it's the escrow account of the Real Estate Company your buyer's agent is affiliated with. Sometimes the listing agent requests for it to be put into their broker's escrow account or for it to be held at the title company.
In any case the earnest money deposit will show up as a credit to you on the settlement closing costs sheet. Thus, your cash needed to close will be reduced by the amount of your earnest money deposit.
As a side note, your lender will request a copy of your cancelled check to include in the loan package.
What happens if you never make it to the closing table?
If the contract falls apart then both sides have to sign a release form. Your agent's broker, or listing agent's broker, cannot release the deposit without it.
Should you and the seller not come to an agreement after a home inspection repair request was submitted, or there are appraisal issues or you exercised your right to declare the contract null and void after reviewing (and not agreeing with) the condo or HOA documents, then signing the release form is usually a smooth process (there are always exception to the rules).
However, if you are found (or thought to be) in default of the contract, then it can get more complicated. The seller could claim part or all of your deposit to cover his damages. A signed release agreement in this case would also release you from further liability. If either party refuses to sign and decides to litigate, then the escrow account holder would pay the entire deposit into the court.
Silver Spring, MD Home Buyer FAQ: What happens to my earnest money deposit?