If I hire a freelancer to work on project A and charges me X$/h, but instead work on project B and still charge me for that time, it's illegal. Of course, illegal is a fuzzy term, it really depends of how the contract is crafted and, more importantly, how much one would be willing to take legal action against the freelancer (which will most likely turns out more expensive than just letting it go).
I'm no lawyer, but pretty sure it's not written in standard contracts "Employees can work on whatever he/she wants, get to keep all the IP of it, pending that employee doesn't get caught."