Question: Do publishers give you an advance as some sort of earnest money for your manuscript? If your work doesn't sell very well do you have to give the advance back to them?

Do they ever just buy your work and you get some money plus royalties?

thanks.

Answer: 
**No.** That's the meaning of the word "advance": these are monies that the publisher is willing to give you up-front with the belief that your work will sell enough copies to cover the advance.

Keep in mind that the advance is not "free money": it's a portion of the royalties for your book that you receive ahead of time. You won't receive any further royalties for your book until it's sold enough copies that the royalties you would have earned cover the advance. If your book fails to "earn out" the advance (sell enough copies to cover the advance), you can expect to have a hard time getting another contract with that publisher, and possibly with other publishers as well.
**Maybe**. If, for some unforeseen reason, the book is cancelled between the time the contract is signed and the publication date, the publisher may request that any paid-out portion of the advance be repaid.

For instance, it might happen if the first half of the book is amazing, but the second half is a complete turkey. Or if the second half never gets submitted at all. Or a million other reasons.

But the contract should say what happens, or at least what the publisher is allowed to ask for and when.