Lot's of factors go into it....
- Age & Mx costs
- Dispatch Reliability
- Fuel economy
- Safety
- Suitability, ie is the plane a good fit for profitable routes. If not, even a newer, efficient plane might go bye-bye. Airlines don't like to fly half-empty planes anymore.
Once retired from pax service, some airplanes may end doing cargo. A cargo operator who only flies one leg a day (or one leg per week) can't afford a new airplane, but the fuel efficiency is not that big a deal if the plane doesn't fly much. Better to pay for more gas when you do fly than constantly pay a high mortgage when the plane is sitting around.