This is an insightful question!
Honestly, sometimes it takes a really long time to get images out of your head. But basically, what you do is you learn to "compartmentalize." You think about work while at work, and "forget" about it, as best as possible, when you are not at work. In your training, they will do things to try to "desensitize" you. We went to autopsies, and were shown lots of horrific photographs. They told us it was not uncommon for investigators to get to a scene, look at it, step away, lose their stomach, and come back. It's just part of the job.
When you get home, you can't talk about it, because they really would not understand. So, instead, you spend your time with your family, living in THAT particular moment, not thinking about the past. It takes practice, but it can be done!
As to people in criminal justice having a hard time with family, they told us about the high divorce rate. But, after being in law enforcement for a long time, I began to wonder if perhaps it is not that law enforcement causes divorce, but rather, the people who are drawn to law enforcement don't possess the right skills to be able to successfully maintain a relationship. It's easy to blame it on the job, but I'm not so sure about that! You will need to make time for your family, have vacations on weird days, celebrate holidays the day before or the day after, as you end up working on the holiday, but this is all things that can be done, with proper planning and commitment. You just have to WANT to make it work!