Methotrexate works by inhibiting the enzyme dihydrofolate reductase, which is necessary for the synthesis of DNA, RNA, and proteins. This mechanism helps in reducing the proliferation of cells, making it effective in conditions involving rapid cell growth and autoimmune activity.