Why are people writing complicated kernel code without compensation?
Money is only one way to compensate someone.
Also, how do they make sure that all pieces written by different developers work together nicely when all developers are spread out throughout the world.
Good / strict programming guidelines, testing and trial and error (look at the stable Vs. unstable kernel releases).
Why are companies donating money for open source programs which are then distributed freely to everyone?
Because that doesn't hurt the company as long as they get to reek the benefits of that open source program.
A lot of companies don't give money but instead add a feature *they* need and submit that back to the source-tree for everyone.