Bacteriophages or phages are viruses that infect bacterial cells (for the scope of this review we will also consider viruses that infect Archaea). The constant threat of phage infection is a major force that shapes evolution of microbial genomes. To withstand infection, bacteria had evolved numerous strategies to avoid recognition by phages or to directly interfere with phage propagation inside the cell. Classical molecular biology and genetic engineering had been deeply intertwined with the study of phages and host defenses. Nowadays, owing to the rise of phage therapy, broad application of CRISPR-Cas technologies, and development of bioinformatics approaches that facilitate discovery of new systems, phage biology experiences a revival. This review describes variety of strategies employed by microbes to counter phage infection. In the first part defense associated with cell surface, roles of small molecules, and innate immunity systems relying on DNA modification were discussed. The second part focuses on adaptive immunity systems, abortive infection mechanisms, defenses associated with mobile genetic elements, and novel systems discovered in recent years through metagenomic mining.