In some/all cases booking.com has paid for a certain selection of rooms from the hotel, which it aims to resell, and aims to make a profit when reselling but doesn't always.
It could also be fingerprinting. Maybe you've been sorted into the "starving hacker" advertising bucket so you get a discount from the "totally unknown customer" bucket the front desk is forced to put you into.