Work for hire
The rules on who owns the code an employee writes at work — in practice, nearly always the employer.
In the US it is simple: under the Copyright Act of 1976, a "work made for hire" has the employer as its author from the outset, as if the employee had never existed. Much of Europe starts from the opposite end, with copyright arising in whoever created the work. For software, though, the EU's 1991 Software Directive carved out an exception that member states wrote into national law — Sweden in 1993, as section 40a of its copyright act: economic rights in programs created as part of an employee's duties pass to the employer unless agreed otherwise.
The exception covers economic rights only. Moral rights — to be named, and to object to mutilation of the work — stay with the programmer where the law recognises them. The grey zone is side projects. Code written at home in the evenings is normally your own, but plenty of employment contracts try to claim it anyway, which is one reason some projects make contributors certify that they actually have the right to submit their code.