That's exactly how it works. There's nothing illegal about employees of one company helping out another. If Tesla shareholders don't like it - well, they can vote out Elon at their next meeting. I'm sure that will work out great for them.
Larry take your team of coders, we are going to construction site of my house, I need some free help there and don't want to pay for workers. Sounds odd hmm? Really this is not how it works.
You are assuming free.
Corporations have a fiduciary duty to all shareholders, including minority shareholders - not just the majority. The issue isn't it being "illegal" for "employees of one company helping out another". It's that Elon Musk is an employee (CEO), not the full owner (only a partial owner), of Tesla, and doesn't have the legal right to treat Tesla resources as his personal property.