Sort:  

Ransomware is more of the outcome of a piece of malware. It encrypts your files and demands a ransom for you to decrypt them (so you can use them again). There are many methods that malware can infect a system, like trojans, malicious email attachments, bad websites, etc.

To defend yourself, at a minimum have a quality anti-malware software installed, make sure your OS and applications are patched, use a firewall filter, make off-line backups of critical files, and simply apply common sense (with a bit of paranoia) when clicking, installing, and surfing the Internet. That would be a good start.