While it occasionally makes for good storytelling to say "X was promoted because of Y", that's not actually how it works.
In the United States armed forces, an officer is chosen for promotion to a higher rank based on the expectation that they will satisfactory fulfill the duties of that new rank, not as a way of reward for exemplary behavior. The latter is what medals are for.
The specific process for promoting an officer is something like:
- A commanding officer selects a candidate in keeping with their service's regulations and their own personal experience. An exceptional candidate may be selected despite not meeting all of a service's regular requirements.
- The candidate's selection is referred to the service command (Starfleet HQ), where a flag officer reviews the commanding officer's suggestion with the aid of their own staff and the flag officer's own judgement.
- The service command adds the promotion to a list of similar promotions, which is then given to whatever final authority actually grants the commissions. In the United States, this is Congress; in a monarchy, it would be the king; in the Federation, it's likely either the Federation Council or a governing body within Starfleet.
- If all of the above pass without incident, the promotion is complete and the selected candidate has obtained their new rank. There will likely be a formal ceremony, although such is not always required.
While the ranks and organization are not an exact match, it's probably that Starfleet operations on a similar principle. Lieutenant Riker wasn't promoted to Lt. Commander based on his heroism; he was promoted because Starfleet needed another Lt Commander, and given his service record he seemed up for the challenge.
Note that, if Riker was given a medal or honorarium for what he did, it's likely that he should have expected to receive the same upon his return to the Federation.
And, of course, it's possible for a commanding or flag officer to grant a temporary, or "field" promotion. Or to demote rank as way of punishment. Or assign an officer to a task despite not having the usual rank required. But usually the rules are followed.