The CRISPR-Cas system is able to adapt following bacteriophage attack, allowing it to act as a sort-of adaptive immune system (like that found in humans).
When a bacteriophage attacks a bacterium, parts of its genome are cleaved and inserted into the repeat-spacer array at the CRISPR loci. The phage DNA is inserted as spacers, with the section being inserted being prior to the PAM site in the phage genetic material.
On future phage attack, the CRISPR loci is transcribed, TracrRNA binds to the complementary spacer in the pre-crRNA, allowing the identification of the relevant section. This allows the pre-crRNA to be cleaved to recognise a specific phage DNA by RNAse III.
This tracrRNA-crRNA complex then binds to the incoming phage DNA, allowing the recruitment of the Cas9 nuclease. This forms double strand breaks (DSBs) in the DNA, preventing it from being replicated and thus foiling the attack.
This process conveys immunity to the bacteria against phage attack, allowing it to survive.