One of the most important defenses to copyright infringement is the fair use doctrine. A defendant who can show that her copying was fair use will not be liable for copyright infringement, even if she copied protected elements of a copyrighted work.
The statute which defines fair use gives examples of activities that are likely to be considered fair use, including copying for purposes of criticism, news reporting, and teaching. Courts have also applied the doctrine to other uses including parodies and reverse engineering of software. No use is necessarily fair use under the law - rather, courts must apply a four part test every time they consider a fair use claim. 
The first prong of the test asks about the purpose and character of the use. New uses that are very creative and transformative are more likely to be fair use. New uses that are commercial are less likely to be fair use, although some commercial uses can be fair.
The second prong asks about the nature of the copyrighted work, including how creative the original copyrighted work is. 
The third prong asks about the amount or substantiality of the portion copied. If the defendant copied only a small part of the copyrighted work, this will help support a claim of fair use. But in some circumstances, defendants who copy the whole work can still show fair use.
The final prong is the effect of the copying upon the market for the copyrighted work. Many lawyers think this is the most important prong - if the defendant copied the plaintiff's work and sold it to people who would otherwise have been the plaintiff's customers, a court is unlikely to find fair use. Not all market harm is evidence against fair use, though - a critical movie review that includes clips from the movie can be fair use, even if it stops people from buying movie tickets.
A court considering a case of claimed fair use must balance all of these factors. The outcome of fair use cases is extremely hard to predict.
Fair Use and Reverse Engineering
For traditional media such as books, copyright represents a "bargain" between the author and the public: the author has an exclusive right to make and sell copies, but anyone can look at the book, learn from its ideas, and use those ideas in new ways to "promote the progress of science and useful arts." But for software which is available only as object code, the bargain is one-sided: the author gets a monopoly, but the public has no access to even the uncopyrightable ideas contained in the code. Some courts have used the fair use doctrine to redress this imbalance, holding that reverse engineering is fair use when the defendant only extracted unprotected elements, such as ideas, from the copyrighted work.
Decompiling object code produces an approximation of the original source code. Merely making this rough copy would usually violate the copyright holder's exclusive rights, even if the person who decompiled the code only used it as a preliminary step in making another work. Someone who reverse engineers software may therefore be liable for copyright infringement unless they can show that reverse engineering is fair use.
This issue has come up in several cases involving interoperability of video game components.
So far, the courts have determined that decompilation that is carried out solely for the purpose of gaining access to unprotected functional elements of a program in order to create interoperable systems is legal in some circumstances.  In Sega v. Accolade, Accolade decompiled the operating system of the Sega Genesis game console. In so doing, Accolade discovered an activation key which allowed a game to be executed on the Genesis console (consisting of the letters 'S-E-G-A'), and incorporated that activation key in its games. The court determined that decompiling in order to allow for interoperability was fair use under these circumstances. The ruling was based in part on the conclusion that the 'S-E-G-A' activation key was functional and not protected by copyright. The ruling also relied on the controversial economic conclusion that the games Accolade sold for the Sega console would not harm sales of Sega's own games. A similar situation arose in Atari v. Nintendo, where the court determined that Atari's decompilation was impermissible under the Copyright Act. In Atari, the defendants had not copied an activation key itself, but rather a code sequence for generating a key. The court remarked that while the key might be functional and unprotected by copyright, the more complex code which generated the key was protected, and so its decompilation was improper.
In Sony v. Connectix, Connectix decompiled the Sony PlayStation BIOS in order to build a computer PlayStation emulator, but did not include any code copied from Sony in its final emulation software. Connectix's product would allow the operation of PlayStation games without the purchase of a PlayStation console. A trial court concluded that Connectix's copying was not fair use because it would harm sales of PlayStation consoles. But the appeals court reversed, holding that "The intermediate copies made and used by Connectix during the course of its reverse engineering of the Sony BIOS were protected fair use, necessary to permit Connectix to make its non-infringing Virtual Game Station function with PlayStation games."
Finally, the Copyright Act itself now has a decompilation provision in §1201(f) of the Digital Millennium Copyright Act (DMCA).  Under some circumstances, a person may circumvent access control measures around a copyrighted work, if the motive is to assist in the production of a separate program meant to be interoperable with the copyrighted work, or other programs that depend on the copyrighted work. Many critics have charged, however, that this protection is too narrow and that the DMCA overall threatens developers' ability legally to engage in reverse engineering.