Methotrexate works by inhibiting the enzyme dihydrofolate reductase (DHFR), which is crucial for the synthesis of DNA, RNA, and proteins. By blocking this enzyme, methotrexate effectively prevents the growth and proliferation of rapidly dividing cells, including cancer cells.