This is off-topic for this thread and also oversimplified. There is no doubt that the financial aspects in terms of direct costs may benefit hospitals in having residencies. However, hospitals have numerous reasons for having physician training programs, including both residencies and fellowships. These include:
1. For primary teaching hospitals (for example, all of the largest Children's Hospitals in the US), it is part of their primary mission. It is also important in their community fund-raising for new space and new programs. Very important.
2. Advanced training programs, e.g. neonatology or critical care fellows, provide trainees who stay 24/7 and provide a level of care not provided by NP and PAs.
3. Training residents and fellows creates a natural pool of attendings who will work at that hospital and admit to that hospital when they are attendings.
4. Along with #1 above, having training programs, especially advanced programs is a key part of recruiting academic faculty and research money which the hospital wants and needs for a range of reasons.