If you can’t just run, perhaps because you’re being blocked by zombies, then you can attack him, but he’ll absorb a lot of ammo before he drops and even then he’ll only be fazed for a few minutes, so you still need to run. If he does get you then jab the ‘X’ or ‘A’ button to get out of his grasp more quickly. The best thing to do is simply run away, although Nemesis will often jump in front of you or catch you from behind with his tentacles.
That presents a problem because he’s often wandering around the game world looking for you, and will follow you anywhere except the safe rooms and certain key areas – and even then he’ll still hang around outside. The short answer is you can’t, not under normal circumstances. How do I kill Nemesis in Resident Evil 3?