When I worked at a grocery store, we would throw away groceries that had some defect, like a tiny hole in the packaging and such.
I can understand not giving those away because someone may have tampered with the food. As for things that got close to their expiration date, rather than throwing it away, we would usually mark it down to a cheaper price, and people bought those in droves.
As someone mentioned, if you're going to give food away, you don't want it to be aimed at potential customers. Something like homeless shelters seems harmless enough though.