Would hugging or otherwise comforting a close friend of the opposite gender who was visibly upset/crying be a violation of Negiah (prohibition against touching a member of the opposite sex)?
Rabbi Moshe Feinstein, in Igros Moshe Even HaEzer 1:56 did not want to allow shaking hands, even when the other person offers their hand first, as it is likely considered an affectionate act. Hugging, whatever the reason, seems to be much more of an inherently affectionate act, and would be similarly discouraged.
Even according to those who would permit handshakes, hugging is seemingly worse. Handshakes may be excluded because they do not, by nature, lead to anything more, whereas hugging customarily can be associated with, and lead up to, sexual relations. Thus hugging is more inherently an act of physical attraction, as opposed to shaking hands. This is significant because some (Rabbi Yehuda Henkin, among others) want to allow handshakes on exactly this basis, that handshakes are inherently not a sexual act. They base themselves on the words of the Rambam, לא תקרבו לדברים המביאין לידי גילוי ערווה - do not do things which lead to illicit relations (rough translation), which they understand to exclude the types of things which do not lead to such.
It should be noted that the Rambam's formulation of the prohibition (Hilchos Isurei Biah 21:1) is limited to that which is done as an act of lust:
כל הבא על ערווה מן העריות דרך אברים, או שחיבק ונישק דרך תאווה ונהנה בקירוב בשר--הרי זה לוקה מן התורה
However, it is difficult to assess when an act of physical affection passes that threshold.
It is worth noting first what the Rema writes in Shulchan Aruch Even haEzer 21:5 regarding rules against being served by a woman, etc: "And there are those who are lenient regarding all of these matters...and there are those who say these rules (against touching) were only in private (yihud) but in a place where many people are found, like a bath-house, it is permissible to be washed by a non-Jewish maidservant, and so is our custom. And there are those who say that any (touching) which is not in an affectionate way (hibah), rather his intention is for Heaven, then it is permissible. Therefore, we are accustomed to be lenient regarding these matters..."
Many poskim use this comment of the Rema to allow non-affectionate touching. A nice example (noted in the comments by sam) is the Ben Ish Hai, in his book Od Yosef Hai, on parshat Shoftim #22, where he employs this Rema to justify kissing hands of the opposite sex in greeting in a case where the circumstances make it clear that the kissing is merely to show honor and respect and not an indication of affection.
And more specifically, in the sefer Yaskil Avdi (vol 6, Yoreh Deah #25, subsection 6) Rav Hodaya is asked about the practice of clasping hands and blessing someone in mourning. He concludes: "...And therefore it appears to my impoverished understanding that there is no concern of (the prohibition of) greeting mourners, nor any concern of inappropriate affection (hibah); just the opposite, in a place where this is the custom, one is obligated to behave this way so that he does not appear as if he were separating from the community of comforters..."
So we see that comforting someone is certainly not affectionate (hibah) and the Rema lists many reasons to be lenient in different cases of touching (including bathing!) when the touching is not affectionate.