I've asked myself this before, but I'm not sure. Say that you see someone depressed, severely depressed and they just need a hug. If they're the same gender, that's all fine and dandy. However, if you're not the same gender then you're obviously doing negiah. Now, I understand there's some wiggle room with rabbinic mitzvot (however, Rambam and other halachic commentators claim that negiah is actually a biblical commandment). Could such a touch (even if sexual desire is derived from it) be excused or is it still an avera?
RS quoted a fascinating Gemara above, however there are a lot of ways it's interpreted.
Don't get started on trying to psychologically diagnose "sort of kind of pikuach nefesh", unless you're a trained professional or it's clear this person is a danger to themselves or others (at which point you need to call in the professionals).
Let's try and break down this very messy question a bit.
When some people say "I'm shomer negiah", they mean "I don't touch the opposite gender [other than spouse] at all." To others, it means "I don't do affectionate touching." There's a huge difference between those two.
Non-affectionate touching: The Bait Yosef actually demands the former and prohibits a man from taking his nida wife's pulse even if she's seriously ill. My understanding is that very few people follow this opinion. (Rabbi Yehuda Herzl Henkin observes this was not Rambam's position.)
In a recent yutorah mp3 on nursing, someone asked Rabbi Hershel Schachter about therapeutic touch, e.g. a female nurse who reaches out to hold an elderly man's hand as he's wincing in pain from an injection. Rabbi Schachter is of the opinion that non-affectionate touch is permissible, so this was just a question of whether this touch is "affectionate"; he thought it wasn't and therefore this was a reasonable course of action.
As soon as we get to hugs or kisses though, this becomes a lot less clear. Rambam's classic examples of affectionate touching are hugging and kissing! Trying to differentiate hugs is quite complicated.
I know of stories of good observant Jews who hugged a grieving friend, but don't ask me if that was technically within halacha.
As a matter of policy it's a very bad idea. Rabbi Kenneth Brander warned a group of rabbinic students about setting appropriate boundaries; he said it's likely you'll get a woman in your office who is falling apart and having horrible tragedies in her life and you'd think giving her a hug is the right thing to do -- don't. (YU shiur on "men of tzniut" or "gavrei tzniut" or something like that.)
There's a recording in which Rabbi Aaron Rakeffet Rothkoff similarly addresses this dilemma: "what if you're a psychologist seeing a teenage girl and you know that putting an arm around her will do a tremendous amount of good..." - a voice from the back of the room shouts out - NO!! -- Rabbi Rakeffet replies to his student that he's most likely correct, but it is a difficult situation.
The talmud in Sanhedrin 75a brings a case of a man who had his eyes on a certain woman. He became "madly in love" with her. This increased more on more until the man literally became sick with love of this woman. His heart was broken and his health started to faulter until he was in mortal danger.
The sages came and asked the doctors to investigate. The doctors inspected the man and said that there is no hope for the man unless he has relations with this woman.
The sages ruled "let him die rather than have relations with her."
The talmud asks "let her stand naked in front of him."
The sages ruled "let him die rather than have her stand naked in front of him."
The talmud asks "let her speak to him from behind the fence."
The sages ruled "let him die rather than have her speak to him from behind the fence."
According to Rabbi Yisrael the son of Nachmani the woman was not married!
The Talmud asks "why are we so strict (in not allowing him to even speak to her)?!"
Answer: In order that the Jewish girls should not become drawn to immorality.
(now here's the clincher)
The talmud asks: "Let him marry her?!?!!"
Talmud answers: "He will not be appeased this way... as it is written 'Stolen waters become sweet.' (Mishlei/Proverbs 9)"
In any event, from this piece it seems that there is no compromise even for pikuach nefesh confirmed by doctors.