Ransomware can infect a computer in several ways, including through malicious email attachments, infected software applications, compromised websites, and exploit kits hidden in online advertisements.