All the solid answers posted plus they're now a massive store - they can live on, say, $0.10 profit on a book because they made plenty more on the backpack and tire patch kit and deodorant you also added to your cart because convenience.
Plus no retail rent, no retail staff to pay.
It's like gas stations barely breaking even on gas, but making bank on snacks. Bookstores usually won't have those other product categories to fall back on like Amazon does.