This isn't true in most cases. Amazon does buy stuff wholesale through their Vendor program. Almost all sellers are also buying wholesale apart from manufacturers going direct to consumer.
If it is shipped & sold by Amazon, Amazon already paid for it from their supplier. If it is shipped & sold by SomeOtherStore, generally they bought it already from someone. There are a lot of dropshippers also and that is a different story.
Amazon similarly protects itself with onerous contracts that can force suppliers to eat the cost of unsold products at times, and does whatever it can to avoid buying unproven products.
Grocery stores and other retailers in general can be protected by refund policies and buyback policies so they are not necessarily taking as much risk especially on packaged products with long expiry dates. They can also liquidate stuff that doesn't get purchased to recoup some money.