“My wife and I made an offer on a home. They accepted another offer, but we asked and they said they’d keep us as a backup. But we found out the offer they accepted fell through and they sold to someone else without even telling us. Is that legal?”
From your question, it sounds like you only asked to be a backup offer, but never had it in writing and signed by you and the seller.
If that’s the case, you had no contract with the seller and they were free to sell to the other party.
Next time you want to be a backup offer, put it in writing and get it signed by the seller. Then you’ll have a binding contract.