Just like Wesley's assignment that was previously mentioned. I am not talking about an Ensign being put in Command of a Starship with a Captain as a First Officer. But smaller scientific study or research groups, it can happen. Junior officers need to step up to the plate at some point. And when you are at the bottom, you will likely have a few higher ranks that you are in charge of. An Ensign can be put in charge of say a research group, as their first attempt at leading. Their team could be made of other Ensigns and perhaps a few Lieutenant Junior Grades. There could even be a full Lieutenant in there as well who would be able to mentor or guide the Ensign through their first attempt.
Still, it's only a leadership position over tasks.
There are also times when specialists are assigned to missions. A Mission Specialist may be assigned to work with a Starship. If the Specialist is only a Lieutenant, that Lt. may have special permission to lead or guide the Starship during parts of the mission. This doesn't mean the Lt. has overall command over the Captain or the ship, but for certain or specific tasks, they have some authority. The Captain can still take back total control at any moment they wish, and may even still be giving the commands. But that Specialist is still telling the Captain where to go for missions sake.
You can also have a Junior Officer who is Acting-Captain during a night watch. During that shift, a higher ranking Conn Officer could be on duty as well. The Conn Officer isn't there to test the Junior Officer, they are there to do their own job. And it was probably planned by the Staff so that a Junior Officer could be on duty in that position and still have a senior more experienced officer with them to offer help if needed.
There are other situations where a more junior rank can lead a more senior rank to theirs, as many Starfleet Officer Careers are not based around attaining rank or position outside of their field of study. Say, a Doctor or Councillor, or most people who enter in to a Scientific path. Due to their time in service, they may hold a higher rank, but posses no real leadership training and probably prefer to simply work, in which case they probably have no objection to an Officer that's a rank below them to be giving them orders. Again, the Junior Officers still need to recognize the experience a Senior Officer has, and listen to them when they try to tell you something.
The more senior officers are not always there to pull rank. Starfleet isn't the Marine Corps, and rank is not always the compete judge of authority. Rank to Experience, most likely yes. But most times everyone is there to just work. Being in charge of others for these types of situations is not the same as a Department Head or even the Captain, they are simply the leader for that task or mission who reports and relays information between Staff and the team. Its also a time for Staff to evaluate Junior Officers for future promotion. A Junior Officer being put in to a position like this wouldn't just be a fresh new officer, but probably would be close to promotion and needing to prove themselves in leadership roles.