If you've watched any cop show or cop movie, you can probably recite the warning from memory:
You have the right to remain silent;
If you do say anything, what you say can be used against you in a court of law;
You have the right to consult with a lawyer and have that lawyer present during any questioning;
If you cannot afford a lawyer, one will be appointed for you if you so desire.
That admonition comes from a famous criminal law case, Miranda v. Arizona, and must be given to any person prior to custodial interrogation. But there are three major exceptions to what's become known as the Miranda rule or Miranda rights.
As we noted above, the Miranda rules only apply to custodial interrogation, not before. So until the interrogation has begun, police aren't required to provide a Miranda warning. But what constitutes interrogation? Generally, a request for identifying information is not considered an interrogation, so questions regarding your name, date of birth, and address are allowed without a Miranda warning.
However, once police officers begin asking questions that may implicate involvement in a crime, an interrogation has begun, and a Miranda warning must come before then or any statement you make could be suppressed.
Miranda rules also only apply to the government or "state agents." This obviously includes police officers or prosecutors, and obviously doesn't include private citizens -- so your neighbor Jeff doesn't need to Mirandize you before you confess to stealing his mower. But what about undercover agents or paid informants?
The Supreme Court has held that confessions obtained from undercover agents or informants like jailhouse snitches (even if they're paid by the government) don't violate the Miranda rule because there is no coercion and no police dominated atmosphere since the suspect doesn't know he or she is being questioned by the police.
Finally, statements made during custodial interrogation without Miranda warnings can be used against a defendant if that questioning was "necessary to secure [officers'] safety or the safety of the public." This came to the forefront during the Boston Bombing investigation, when suspect Dzhokhar Tsarnaev was initially interrogated without being read his Miranda rights.
In situations where there is an imminent danger to the public, like suspected terrorist attacks where police are trying to ascertain the whereabouts of a bomb or knowledge of a future attack that's going to take place soon, officers are given some leeway to continue interrogation. Even if the person has invoked their Miranda rights, their responses may still be admissible in court.
And, of course, you can waive your Miranda rights. This isn't recommended in general, and certainly not before you've consulted with an attorney.