Of course, Apple spends huge amounts of money on developer infrastructure (Xcode, LLVM, and Swift, for example). Since each additional copy of a piece of software does not incur additional costs for the developer, Apple can subsidize the development of developer tools using the $99 fee taken from (generally) professional developers who wish to publish their apps on the App Store or take advantage of notarization on macOS.
Many major open-source projects are largely developed by employees of large technology companies and foundations with significant budgets. The Linux kernel's largest committers oftentimes work at companies like Red Hat and Intel. Just because you get a given piece of open-source software for free does not mean that its development was done by volunteers in their free time.