Companies are doing this in order to make some product or platform (on which they earn a profit) more attractive. For example Apple is developing Swift in order to make iOS development more attractive for developers. The hope is of course more iOS developers means more apps which in turn makes the platform more attractive for consumers, which means Apple will sell more iOS devices and earn more money. Pretty simple.
In other cases the profit motive is more indirect. For example Sun developing Java. Sun had their own hardware, but Java was deliberately cross-platform. The reason was Windows at the time had a near-monopoly on desktop platforms which meant developers only developed software for Windows, which made other platforms like Suns less attractive for consumers. A vicious circle from the perspective of Sun. The hope was Java would encourage developers to write cross-platform software, thereby making the non-windows platforms more attractive for consumers.
.Net was developed as a counter-measure from Microsoft, since Java attracted many developers because it seemed more modern than Microsoft's offerings (VB and C++). Microsoft therefore tried to deliver a similar modern platform but still tied to the Windows platform.
If you look into other platforms and frameworks you can see the same forces at work again an again.
The change in the .Net strategy (from Windows-specific to cross-platform) is clearly because Windows have lost in the mobile platform market. .Net was created to solidify a virtual desktop monopoly, but now the goal is basically the opposite, to create a cross-platform environment to fight the lock-in by iOS and Android. So basically Microsoft has the role Sun had, and Apple has the role Microsoft had.
Google and Facebook have pushed HTML and JavaScript heavily. This has primarily been to make the web a stronger platform and to make it more attractive compared to platform-specific development which advantage the platform-owners like Microsoft and Apple. Microsoft used to fight the web and sabotage web standards, but after they lost the battle for the mobile platforms they (not surprisingly) have become great proponent for the platform-independent web, and even work together with Google on Typescript.