You're talking about when books are marked down at a book store or other retail outlet, right?
The short answer: the retailer. They've spent money to acquire those books, usually at a discount anyway if they ordered in bulk, and they put them on sale when they have ascertained that the money they'll lose by not selling them full-price will be less than the money they'd lose by not freeing up the shelf space to sell something else at full price. Or, to be more accurate, when they've ascertained that they'll potentially make more profits by selling something else in that shelf space than they'll make by trying to sell your book at full price.
Retail balance sheets are almost a science in and of themselves.
The long answer: Everyone involved, to a greater or lesser degree. Happily, the author gets the lesser degree. When you, a published author, get your statement of earnings from your agent or publisher, you will see a column called "Reserve Against Returns." That's not returns from readers -- the retailer deals with those. Those are returns from the retailers, who, if they can't sell your slow-moving book at a discounted rate, will return the copies to the publisher (to be destroyed by the publisher) for part of their money back.
Every book deal includes some mathematical factoring for expected reserve against returns. In order to earn out your advance, you must achieve the expected number of sales and your returns from retailers can't exceed the reserve.
It's fairly complicated, so you probably shouldn't trouble yourself much about it beyond realizing that it's factored into your advance.
(I work in a book store currently.)