Ransomware is the name given to software that encrypts or deletes files on a computer and will restore them to the user in exchange for payment to the person who created the software.
It is most commonly spread by malicious email attachments, although it can be embedded in downloaded files, install when you visit a web page or even be transmitted to an unsecured computer when it connects to the internet.
The most common forms of ransomware are Microsoft Office files that contain macros that infect Microsoft Windows machines with unpatched vulnerabilities. Usually, these vulnerabilities are known and limited to older versions of the software or a more up to date version that hasn't had security updates installed.
With nearly all ransomware targeting Windows machines via Microsoft Office files, the most obvious way to protect yourself from this is to run a different operating system. Although there is malware that targets Mac OS and Linux, it is much more uncommon, and there has yet to be a large-scale case of ransomware on these systems.
If you do have to run Windows, then it is best to run the most up to date version, as Microsoft has stopped all support for Windows XP and Vista (although they did release a patch for the recent Wannacry ransomware as it was an exceptional case) and will stop supporting Windows 7 in 2020.
Even if you do run Windows, you don't necessarily have to run Microsoft Office, as there are many alternatives available, some of which won't be vulnerable to the same sort of attacks. There are installable office suites where you can disable macros, and you can use cloud-based office suites that should be impervious to this form of attack. If you do run Microsoft Office, it is worth trying to disable some macros unless you know and trust the file and keeping the software patched just as you would your operating system.
Of course, a huge factor is preventing infection in the first place. Only download and install files that you trust, and make sure to run them through a virus scanner before opening them. If someone sends you a file unexpectedly, or someone you don't know sends you a file, treat it with caution. Another good practice is to check that the text of the email refers to the attachment and the reason for having the file.
As well as keeping your software up to date, it is also a good idea to have a firewall and anti-virus software. These will prevent people being able to access your PC, and the files that you download and install or run, from being able to damage the computer.
Parallel to these precautions, you should make sure that all important data is backed up. With the availability of cloud storage systems and the low price of USB flash drives, there is no reason to have only one copy of any file. Even if security is a consideration, it is easy to make sure that any copies are only known to or accessible by you and it can save you from losing data to ransomware (or many other disasters).
Finally, if all else does fail, don't panic. If your machine is infected, try to leave it on and don't reboot it, as often the ransomware will only work upon restart. Search for an online solution and resist the urge to pay the ransom. However, in the case of Wannacry, the maker had no real way of telling who had paid the ransom, so it may have been possible to bluff that you did pay the ransom. You should never rely on a bluff, however.
With a little planning, regular maintenance and minimal outlay you can make yourself invulnerable to most attacks and mitigate the effects of all of them. It is possible to ensure that you are never caught out or out of pocket to this rising threat.