In a traditional navy structure there are "line officers" (US term also used in TOS) who are trained for commanding vessel. They are called that because there is a "line" of succession from the most senior to the most junior. On a ship, the captain and executive officer are at the top, and so on down through the ranks by seniority to the lowliest line ensign.
There are others whose training is in more specialized areas and can never command a ship regardless of their rank. These would be supply officers, physicians, nurses, dentists, chaplains, JAG officers, intelligence officers, bandleaders and so on. They generally take command of personnel only in their immediate staffs, but senior officers may take command of activities and installations ashore.
The US Navy counts engineers (in the shipboard propulsion sense) as line officers, the British Royal Navy and many others do not. Starfleet seems to follow more of the US practice, with engineers eligible to attain command of vessels.
A clearly unqualified Troi taking charge in "Disaster" was silly. But when Troi took command on the bridge in "Thine Own Self," it seems she was acting as what the USN would call "Officer of the Deck," who is in charge for that particular watch but is a position that can be filled by even a junior officer if qualified. It doesn't seem to have changed her position in the chain of command, that is, Data was still third in line and a lot of other officers probably ahead of her. Not something that would happen with a psychologist/counselor in the present day, but apparently Starfleet allows more leeway for cross-training in that kind of situation. Really being in charge for a few hours of cruising in a straight line through empty space shouldn't be too demanding; if anything happens the captain and XO are just a minute or two away.