^ I could be remembering wrong, but I think real-world naval regulations make it hard for a commanding officer of one ship to take command of a different ship in the first place the way Decker did, regardless of his rank.
You're right, command of a ship is succeeded to through its chain of assigned line officers. A captain of one ship can't take command of another. (However, if two ships meet up and operate together without an officer aboard higher than their COs, the senior captain will hoist the green and white pennant Senior Officer Present Afloat and can give orders to the other captain).
The rules (US, at least) are different for flag officers. A flag officer who is qualified for command at see can give orders to the CO of any naval vessel, even if the he/she is only aboard as a passenger, if there is no senior flag officer present. This makes perfect sense: If something goes bad, you would want the most experienced officer to take charge, and flag officers eligible to command at sea all (except maybe a few aviation officers) commanded ships earlier in their career.
Oh I agree.
Commodore Decker was captain of the Constellation. However, he was possibly the equivalent of a flag officer, he outranked Kirk, and he was on the Enterprise when her captain was absent.
Starfleet commodores are flag officers, that is confirmed in "The Deadly Years."
Of course these kinds of things are set aside for dramatic license. Objectively, if you have a big Starfleet doing great things all over the place, it must have very professional, qualified and effective leaders. But the ones we meet have to be foils to Kirk and Co, so they are petty, corrupt, ineffective, broken etc. If the senior officer just took charge and set everything right, like you would expect would really happen, well, where's the fun in that?