An epigenetic process in which methyl groups are added to a cytosine residue of DNA, mainly at cytosine-guanine dinucleotides (CpG). This affects gene regulation in two ways: by directly blocking the binding of certain gene-activating factors and by promoting chromatin compaction, which affects the ability of genes to be switched on or off.