I recently finished writing my first novel. Many of the characters resemble people in real life, and they are not always portrayed in a flattering manner. Should I put in a disclaimer that it is a work of fiction and all resemblance to reality is purely coincidental?
I won't pretend to be an expert, but I would be inclined to say yes as it sounds like the "resemblance" is strong and that it's not a coincidence.
Yes, you should put a disclaimer. I am not a lawyer, but I have read about defamation cases: Apparently the key to winning such cases is whether reading the book provides enough detail to identify the real-life person upon which the character is based (and whether damages were actually suffered, and a reader knowing the real-life person would have a reduced opinion of them as a result).
For example, a university professor has a public life and a job that can be in jeopardy for moral turpitude, and one in which reputation matters amongst both colleagues and students. If students don't sign up for the professor's classes, he may get stuck teaching Freshman prerequisites which impact his ability to do research and recruit graduate students as an advisor. Caution among colleagues can reduce his participation in funded projects. Caution among organizations that normally fund academic research may make it harder for him to get grants; the people awarding grants tend to have great discretionary powers to find "merit" as they see fit, and are impossible or difficult to challenge. A career can be in the balance.
Say one of his students publishes a fictional account in which her professor drugs her and rapes her, and his colleagues and his other students reading the book (and a jury reading such passages) can see she clearly bases this criminal on our innocent Professor. Disclaimer or no, He has a defamation suit against her.
You should read up on defamation, and be aware that simply including a disclaimer is not a stone wall defense. Just because you wrote it does not make it true; a judge or jury can conclude that the preponderance of evidence shows you were indeed writing about a real person and defamed them.