Each person has to handle it differently. My first (real) patient death, I actually had a talk with one of the ICU docs who recommended taking some alone time before going back to the family. He explained how he rationalized death (they are always really sick when they go to the PICU) and avoided certain fields (Heme/Onc) where there was continuity, but some people find comfort in that.
Several residents I know, including myself, have stories about a patient that went downhill despite our best efforts. It can be extremely emotional, and I've cried, I've held family's hands, I've avoided circumstances where I might encounter the family again. But, the family is almost always going through something worse, which helps ground me. I actually ran into the mom of one of my 'bad outcome' patients today, and she expressed gratitude for me being there in the beginning, despite my feeling guilt over not catching what was going on with him sooner.