Where I work, there are a number of inventory managers whose job (among other things) is to monitor the rates of movement of their titles through retailers. If any fall below a certain threshold, the inventory manager will make a recommendation to the publisher to remainder those titles. The publisher reviews these recommendations and, if he or she agrees, those titles are officially remaindered. Generally, the first step is to offer the unsold copies to the author at unit cost; that is, the author has the opportunity to buy his or her unsold stock and do whatever he or she wants with it: try to sell it him/herself, donate the lot to charity, fill a water tower with it and swim around in it like some kind of bibliophilic Scrooge McDuck. The sky's the limit!
If the author doesn't want the unsold copies, however, then the books are auctioned off to retailers that specialize in remaindered books, such as Crown Books (not to be confused with the Random House publisher of the same name). As mentioned, the publisher loses some money (but not as much as they would by simply pulping the stock), makes space in the warehouse for titles that are moving, and savings are passed on to the retailer and you, the customer. The publisher loses out a little bit, and the author loses out a lot (since, as far as I know, no royalties are issued on sale of remaindered books).
Clearly, all authors want to avoid having their titles remaindered, but the sad truth is: there's nothing you can do about it besides write a killer book and do whatever you can to help sell it. Your efforts are often necessary to make your book a hit, but almost no amount of effort on your part can save a book if it's just not taking hold in the market. Not unless you and Oprah are BFFs.