Just to add to the other comments here, from a more freelance perspective:
Most work for large corporations is done under a "work for hire" agreement which means the company owns the copyright to the source you write. Additionally, many big companies will pressure you to accept employment clauses that claim ownership of anything you create in your free time, 24/7. This is not just for code, it's also standard for contract employees in the TV and film industry.
Note I'm distinguishing here between "employee" and "freelancer". I refuse to sign work-for-hire agreements, which means when hired, I still own the copyright to the source. I grant the company hiring me free use and modification and in some cases, right to resell.
I had a case of a startup where I was the sole coder, who turned around and sold their app to [Fortune 100 company] who didn't realize that I still owned this source. This company then asked me to consult - offering half what I'd been paid to write it - and further, asked me to sign a contract stating that they owned the code and everything else I made while I was consulting with them. Oh and also, without my help, they were missing some major assets they needed to recompile it.
I told them where to stick it, so they hired a bunch of people to try to reverse engineer it rather than agreeing to my terms. That didn't work (not least because I had trapped the fuck out of the code they did have, in ways that made it crash the compiler) so they ended up down a couple million bucks and shuttering the site. If they had only been nice, and hadn't come at me with the aggressive attitude, it could've been a nice little asset.
If you do not sign a work-for-hire agreement, and you're an independent contractor - not an employee - you own the source code unless otherwise stated in a contract. Beware that some jurisdictions will view you as a de facto employee in some cases (for better or worse). And my advice to anyone listening is, never, ever sign a contract with one of these companies that stipulates that they own your off-time work product. It's completely abusive and unnecessary. It's not worth taking a job with such a company.