The fast food restaurants throw the food away at the end of the day if they have food leftover that didn't sell because they don't want to risk being sued by someone should they donate or give the fast food away to someone or charity.
A fast food restaurant can legally give leftover food away to homeless people or charities or anyone else if they want too but they don't do so because of possible legal troubles.
Even though the food is still good they don't want to risk a lawsuit even though all that food going to the landfill is a waste when people are going hungry.