In the United States, if you are paid for the work of creating the program, then the program is known as a "work-for-hire" and the employer owns all the rights to it. The paid worker who wrote the code does not retain any rights to it. In general, if the code was written to the specification of the purchaser, then that is a work-for-hire.
If the transaction was instead the sale of a software license, then the original author retains the copyright.
Courtesy the somewhat out-of-date Wikipedia article on works-for-hire, here is the citation from U.S. Copyright Law:
U.S. Code, Title 17, Section 101 "Definitions":
A “work made for hire” is —
(1) a work prepared by an employee within the scope of his or her employment; or
(2) a work specially ordered or commissioned for use as a contribution to a collective work, as a part of a motion picture or other audiovisual work, as a translation, as a supplementary work, as a compilation, as an instructional text, as a test, as answer material for a test, or as an atlas, if the parties expressly agree in a written instrument signed by them that the work shall be considered a work made for hire. For the purpose of the foregoing sentence, a “supplementary work” is a work prepared for publication as a secondary adjunct to a work by another author for the purpose of introducing, concluding, illustrating, explaining, revising, commenting upon, or assisting in the use of the other work, such as forewords, afterwords, pictorial illustrations, maps, charts, tables, editorial notes, musical arrangements, answer material for tests, bibliographies, appendixes, and indexes, and an “instructional text” is a literary, pictorial, or graphic work prepared for publication and with the purpose of use in systematic instructional activities.
Some written agreements contain terms which specify that the work in question is a work for hire. Even when the work is clearly a work-for-hire, these terms are included to reduce the burden of proving that fact at court. Once the agreement is made, a contract is created which defines the work as per the agreement terms. If there are no agreement terms relevant to the ownership of the work, then the terms from U.S. Law become part of the contract.
There is a distinction between the words "agreement" and "contract." An "agreement" is a (usually written) set of terms which define various acts or behaviors that all parties have consented to perform. A "contract" is an agreement plus the laws which govern it.
For example, an "End-User License Agreement" intends to create a contract with the end user. That contract, if formed, consists of the terms of the agreement, plus the applicable laws such as the Uniform Commercial Code (UCC) and the Digital Millenium Copyright Act (DMCA).
I am not a lawyer. Depending on your reasons for asking, you may be much better off asking one.