A large part of my job is understanding the culture of companies and the software engineers best suited to join them; in doing that, one metric is the age of the individuals, because it does play a role in the hiring decision. It's unfair, but age does become a factor at a certain point, just around the 50-55 point as a software engineer at a decent amount (but definitely not all) companies. But your statement "nobody will hire you after 40" is false - software engineers in their 40's will be welcomed at 90%+ companies. Perhaps you're in San Francisco and experiencing more ageist attitudes inside the latest young bro-filled startup bubble, but this represents a small minority.
Second, literally every for-profit job involves making money for your bosses; this is how capitalism works. If you want to make more money tired to your own contributions, then you either climb the ladder - through merit - to become a boss or run a successful business. These are the facts of life that one should know by the age of 40.