I think it's because you can put your emotions to one side when you need to. It's not that you don't get upset or angry or whatever, it just that you know that to do your job properly you have to be thinking clearly. You deal with the emotions later, once the things you need to do have been done. A lot of it is in the training, but I have not found it a skill that has been difficult to learn. Sometimes it is ok to show some emotion- being sorry that a patient you know quite well or have looked after for a while is now heading towards the end of life, for instance, can be appropriate. Being in floods of tears would not be, but showing that you are genuinely sorry can be helpful for the patient, the family and yourself.
I talk to close family- my mum especially, but my partner too. It's not that seeing death or severe illness and distress don't upset me, they can do, but I am able to put my feelings to one side when it is needed.
Another thing that helps me cope is to see it as my job to make sure that my patient has the best care, accepting not everything can be fixed and helping them manage their symptoms so they can live as well as possible right until the end of their life, when it becomes my job to ensure they have as good and peaceful a death as possible. In cases of sudden illness/deterioration, it's the same principle.
Actually, I wish we as a society were more accepting of death- especially in the frail elderly- and would let them go without trying to put them through all sorts of investigations or treatment "just in case". I do see this at work and quite often it is more about what the family wants than what the person wants or is in their best interest. I have had patients tell me they wish their family would let it be as they "have had enough" but feel they ought to keep trying new things or fight on because their family are so distraught at the prospect of their loved one "giving up". I actually find it harder, emotionally, dealing with that situation than one where someone is clearly dying or dies suddenly and it's a shock for their family.