Very simple: part of your employment contract requires you to assign to the
company copyright and ownership of any original works produced, modified, or
published by you during the term of your employment. Unless you
specifically negotiate one, there is usually no exemption for things done on
"personal time". They do provide a place where you can declare any works
you produced or owned prior to entering their employ; anything on that list
is exempt from the assignment, but anything not on the list becomes a "work
for hire" on behalf of the company if you so much as think about it while
you're employed there.
It may depend on the state. In California we have labor code 2870. Other
states may have similar laws protecting the individual. You can search for
it on google.
<Quote Description="California Labor Code Section 2870-2872">
2870.
(a) Any provision in an employment agreement which provides that an employee
shall assign, or offer to assign, any of his or her rights in an invention
to his or her employer shall not apply to an invention that the employee
developed entirely on his or her own time without using the employer's
equipment, supplies, facilities, or trade secret information except for
those inventions that either: Relate at the time of conception or reduction
to practice of the invention to the employer's business, or actual or
demonstrably anticipated research or development of the employer; or Result
from any work performed by the employee for the employer.
(b) To the extent a provision in an employment agreement purports to require
an employee to assign an invention otherwise excluded from being required to
be assigned under subdivision (a), the provision is against the public
policy of this state and is unenforceable.
2871
No employer shall require a provision made void and unenforceable by Section
2870 as a condition of employment or continued employment. Nothing in this
article shall be construed to forbid or restrict the right of an employer to
provide in contracts of employment for disclosure, provided that any such
disclosures be received in confidence, of all of the employee's inventions
made solely or jointly with others during the term of his or her employment,
a review process by the employer to determine such issues as may arise, and
for full title to certain patents and inventions to be in the United States,
as required by contracts between the employer and the United States or any
of its agencies.
2872
If an employment agreement entered into after January 1, 1980, contains a
provision requiring the employee to assign or offer to assign any of his or
her rights in any invention to his or her employer, the employer must also,
at the time the agreement is made provide a written notification to the
employee that the agreement does not apply to an invention which qualifies
fully under the provisions of Section 2870. In any suit or action arising
thereunder, the burden of proof shall be on the employee claiming the
benefits of its provisions.
</Quote>
(2870)(a) suggests that if you create code with your own resources, it is
yours to keep. But (2870)(b) seems vaguely worded and appears to contradict
the previous statement.