Anyone who is charged with a crime has a Fifth Amendment privilege against self-incrimination. This means that they cannot be forced to reveal incriminating information about themselves, even if they are asked by law enforcement or in court. The privilege against self-incrimination covers not only responses that are directly incriminating but also responses that could lead to the discovery of incriminating evidence. Sometimes a prosecutor may bargain with someone who is suspected of a crime if they are believed to have information about other criminal activity. In exchange for testifying against someone else, the witness may receive immunity from prosecution. Immunity comes in different types and may have restrictions, so it is important to understand the specific deal that a prosecutor is offering before agreeing to it.
Also known as total immunity, transactional immunity provides a shield against any future charges based on any matter related to the testimony. (The prosecution still can bring charges against the witness for matters that are unrelated to the testimony.) The federal criminal justice system does not offer transactional immunity, but many states do.
The other main type of immunity is known as use and derivative use immunity. This comes with more restrictions and thus is more often offered by prosecutors. Use and derivative use immunity protects the witness from having the prosecution use their statements or any evidence discovered from their statements against them. Essentially, this produces the same result as if the witness invoked their Fifth Amendment privilege and did not testify at all. It allows the prosecution to bring charges based on the same crime against the witness, as long as the charges are based entirely on independent evidence from a different source. Whether that evidence is sufficient to convict the witness without using their statements can be left to a judge or jury to decide.
Typically, a prosecutor offers immunity to someone who has committed a minor crime because they believe that it will help them catch or convict someone who has committed a major crime. Often, it arises in the context of organized crime, when the prosecution offers a subordinate immunity in exchange for testifying against the leader of the enterprise. If you agree to this type of deal, you must testify as promised, or you may face jail time and fines.
Sometimes a prosecutor will bring charges against a witness who has been granted immunity. The defendant can raise the defense of immunity by producing evidence to show that they received immunity with regard to the subject matter of the charges. The prosecution then must explain how they acquired all of their evidence. A blanket statement that they did not use the immunized testimony or evidence arising from it will not go far enough. Any evidence that the prosecution acquired from the immunized testimony likely will be excluded from consideration at trial. If excluding this evidence means that the prosecution has no case, the court will dismiss the charges.
In rare cases, a former witness can waive immunity after receiving it. This allows the prosecution to bring charges against them based on their testimony. The waiver can be explicit, which means that the witness signs a written waiver. Or the waiver can be implicit, which may happen when a witness makes a voluntary statement to law enforcement without claiming immunity. An individual sometimes will implicitly waive immunity in advance by choosing to testify rather than asserting their Fifth Amendment privilege.