There is an interesting article in Money Magazine this month that helps explain it a little bit. They took a bestselling hardcover novel; in this case they took The Associate by John Grisham and broke down what the various costs are. These costs are obviously estimates, and will differ greatly depending on print run but I think it is really interesting to see the basic breakdown.
Based on a list price of $27.95
$3.55 - Pre-preduction - This amount covers editors, graphic designers, and the like
$2.83 - Printing - Ink, glue, paper, etc
$2.00 - Marketing - Book tour, NYT Book Review ad, printing and shipping galleys to journalists
$2.80 - Wholesaler - The take of the middlemen who handle distrobution for publishers
$4.19 - Author Royalties - A bestseller like Grisham will net about 15% in royalties, lesser known authors get less. Also the author will be paying a slice of this pie piece to his agent, publicist, etc.
This leaves $12.58, Money magazine calls this the profit margin for the retailer, however when was the last time you saw a bestselling novel sold at its cover price.
Most books are sold to retailers at X% discount of the cover price by the wholesalers. The size of X pretty much directly corolates with the size of the print run (basic economies of scale), on a bestseller like Mr. Grisham the discount is estimated at about 50%, so we can assume a midlist novel might be 20-30%
From this number take away the consumers discount, as well as staffing, marketing, and rent costs the retailer pays before you get to the actual profit margin.
While it's not exact it does take away some of that "WHAT!?! $30 for a book?" shock I have when browsing my bookstore.