A counselor was a position aboard Starfleet vessels and installations, usually held by a senior Starfleet officer with training in psychology and psychiatry, with the intention of helping their patients with overall mental wellness.
Qualified psychiatrists served aboard starships during the 23rd century. (TOS: "Where No Man Has Gone Before", "Dagger of the Mind") Occasionally, the role of chief medical officer and counselor were combined in the 23rd century, with medical officers also having expertise in space psychology; an example of this is Doctor Leonard McCoy. (TOS: "Court Martial")
By the mid-24th century, starship and starbase crews included a counselor, who was responsible for the mental well-being of the crew and civilian staff. The position was considered vital enough that it warranted inclusion in the senior staff of the Federation flagship; in that instance at least, the ship's counselor also had a diplomatic role, advising the captain in first contacts and other situations.
As with the chief medical officer, the ship's counselor has the power to relieve other officers and crewmembers of duty if he or she feels that their patient is suffering from a condition that may hinder their ability to perform their duties effectively, including the ships captain.
At the captain's discretion, the ship's counselor may be allowed to wear something other than their normal Starfleet uniform. This was the case with Deanna Troi, the ship's counselor on the USS Enterprise-D, who preferred to wear colorful, casual clothing while on duty. Troi elected to wear a standard uniform permanently after being told to do so by Captain Edward Jellico. (TNG: "Chain of Command, Part I")
Lieutenant Commander Christopher Hobson of the USS Sutherland remarked that no one would ever suggest a Klingon to be a good ship's counselor, as he considered them unsuited for such a position. Some Starfleet officers, such as Captain Lisa Cusak and Chief Miles O'Brien, disliked the concept of a designated ship's counselor, preferring to seek advice about their problems from friends. (TNG: "Redemption II"; DS9: "The Sound of Her Voice")
Deanna Troi trained in psychology before joining Starfleet. She served as counselor aboard the USS Enterprise-D and the USS Enterprise-E before transferring to the USS Titan with her husband, William T. Riker. (TNG: "Encounter at Farpoint"; Star Trek Nemesis; LD: "No Small Parts")
Deep Space 9 had several counselors during its time in operation, including Telnorri and Ezri Dax, who was previously an assistant counselor on the USS Destiny. (DS9: "Hard Time", "Shadows and Symbols")
The USS Voyager was not originally assigned a counselor, as the nature of its mission didn't require one. (VOY: "Phage", "The Cloud") Later, Neelix appointed himself morale officer, a position of his own invention, to take on the responsibilities of a counselor. (VOY: "The Cloud") The Doctor later began developing a psychiatric subroutine to make himself "even more valuable" to Captain Kathryn Janeway. (VOY: "Repentance")