Restriction modification system
The restriction modification system (RM system) is used by bacteria, and perhaps other prokaryotic organisms to protect themselves from foreign DNA, such as bacteriophages.  This phenomenon was first noticed in the 1950s.  Certain bacteria strains were found to inhibit (restrict) the growth of viruses grown in previous strains.  This effect was attributed to sequence-specific restriction enzymes.
Bacteria have restriction enzymes, also called restriction endonucleases, which cleave double stranded DNA at specific points into fragments, which are then degraded further by other endonucleases. This prevents infection by effectively destroying the foreign DNA introduced by an infectious agent (such as a bacteriophage). Approximately one quarter of known bacteria posses RM systems and of those about one half have more than one type of system.
Restriction enzymes only cleave at specific sequences of DNA which are usually 4-6 base pairs long, and often palindromic. Given that the sequences that the restriction enzymes recognize are very short, the bacterium itself will almost certainly have many of these sequences present in its own DNA. Therefore, in order to prevent destruction of its own DNA by the restriction enzymes, the bacteria marks its own DNA by adding methyl groups to it. This modification must not interfere with the DNA base-pairing, and therefore, usually only a few specific bases are modified on each strand.

# Types of Restriction Modification Systems
There are three kinds of restriction modification systems: type I, type II and type III, all with restriction enzyme activity and a methylase activity. They were named in the order of discovery, although the type II system is the most common.
Type I systems are the most complex, consisting of three polypeptides: R (restriction), M (modification), and S (specificity).  The resulting complex can both cleave and methylate DNA. Both reactions require ATP, and cleavage often occurs a considerable distance from the recognition site. The S subunit determines the specificity of both restriction and methylation.  Cleavage occurs at variable distances from the recognition sequence, so discrete bands are not easily visualized by gel electrophoresis.
Type II systems are the simplest and the most prevalent. Instead of working as a complex, the methytransferase and endonuclease are encoded as two separate proteins and act independently (there is no specificity protein). Both proteins recognize the same recognition site, and therefore compete for activity. The methyltransferase acts as a monomer, methylating the duplex one strand at a time.  The endonuclease acts as a homodimer, which facilitates the cleavage of both strands.  Cleavage occurs at a defined position close to or within the recognition sequence, this producing discrete fragments during gel electrophoresis.  For this reason, Type II systems are used in labs for DNA analysis and gene cloning.
Type III systems have R and M proteins that form a complex of modification and cleavage. The M protein, however, can methylate on its own. Methylation also only occurs on one strand of the DNA unlike most other known mechanisms. The heterodimer formed by the R and M proteins competes with itself by modifying and restricting the same reaction.  This results in incomplete digestion.  

# Uses
RM systems can be cloned into plasmids and selected for because of the resistance provided by the methylation enzyme. Once the plasmid begins to replicate, the methylation enzyme will be produced and methylate the plasmid DNA, protecting it from a specific restriction enzyme.
Some viruses have evolved ways of subverting the restriction modification system, usually by modifying their own DNA, by adding methyl or glycosyl groups to it, thus blocking the restriction enzymes. Other viruses, such as bacteriophages T3 and T7, encode proteins that inhibit the restriction enzymes.
To counteract these viruses, some bacteria have evolved restriction systems which only recognize and cleave modified DNA, but do not act upon the host's unmodified DNA. Some prokaryotes have developed multiple types of restriction modification systems.