Troi was in command because she held the rank of Lieutenant Commander after the helmsman who "had the bridge" was killed.
O'Brien was enlisted and Ro was an ensign, somewhat of a disgraced one at that. The helmsman was in command until being killed, in lieu of there being no "second in command" (the other bridge officer was killed too) command went to Troi due simply to rank.
But one's position
on the ship mattered more than one's rank. At the end of "Thine Own Self" Troi snarks at Data that he can call her "Sir" now that she had her Commander pip. This is a bit inaccurate as Data was still the second officer so Troi in matters of ship operations was still Data's subordinate. (Data was Second Officer and Troi, technically, wasn't in the chain of command, she wasn't even really a bridge officer and was just often around for her empathic skills when they were needed.)
At the most basic level Picard, Riker and Data were the only ones "in command" of the ship. (In reality Data should have been wearing red, but he was considered the "Operations Officer" so he was in command of pretty much all yellow-shirted personnel.)
This is sort of how I saw the Chain of Command on the ship:
This is under normal conditions, so not during a medical crisis where one's knowledge was critical to the situation.
I saw Picard and Riker as more or less on equal footing. Picard was in charge, of course, but it was Riker's duty to agree with Picard's decision and carry them out.
Data was the Operations Officer and, thus, in charge of the operation of the ship which at the very least consisted of Engineering and Security. So, in theory, Worf and Geordi would report to data about issues in their department and Data would take that to Riker. Data was also sometimes called the "Science Officer" which may also mean he over-saw the duties of the various science departments as they needed to work on projects. Data would co-ordinate with security and engineering to see that the science officers got their needs met. All would report to him.
Crusher was in charge of the medical staff (which may have included mental health) so she was technically "over" Troi but they were probably more or less on equal footing as well. Both were in charge of their respective departments, assuming there was any more counseling staff beyond Troi.
The helmsmen on the ship, strictly speaking, were only subordinate to Picard and Riker as they're the only ones who can tell the ship where to go and being a helmsman seems to be in the Command department of the ship and Riker was in charge of the command-chain officers.
Then we have Chief O'Brien. Though never explicitly stated on screen I sort of think he was a bit more than the Transporter Chief and was also the "Chief of the Boat", in charge of all the Non-Commissioned officers. But considering Non-Coms were rare in Trek it's possible there weren't many to manage and any other Non-Coms were just beholden to those in whatever department they were in.
There's some cross-over here at times, obviously. If Data's on the bridge acting as the OPS officer and he needs the Helmsman to do something he can make that request and being Second Officer the helmsman would have to do it, and of course whomever "has the conn" is effectively in Picard's position at top and in charge of everyone.
The "Helmsmen" at the bottom pretty much is the word I used to describe all of the one-pipped red-shirts roaming around the ship who obviously had duties to do somewhere, even if it wasn't driving, but not apparently in either the Operations or Medical/Science departments due to the red shirts.