1) You get to a place the rules say you can hide (out of sight, usually, though other cover works for some special circumstances, like the lightfoot halfling).
2) Use an Action to Hide (roll a Stealth check). Rogues often use their Cunning Action to do this.
3) If your Stealth > your target's Perception (usually passive unless they get actual actions to look for you), then you may attack from a hidden spot with Advantage. In my game this includes popping out from around a corner, just enough to take a quick shot. If you've ever 'sliced the pie' with a weapon, you'll know what I mean.
4) Once you resolve your Attack Action, you are no longer hidden (exception - Skulker, if attack missed). However remaining movement and a Cunning Action to hide may be an option, even after an attack (assuming a rogue shot from a pre-existing hidden spot).
The hidden character is NOT jumping into clear space to take a shot ("TA-DAAAA!"), but shooting with minimal exposure from concealment. Any one who enters an 'open' space in view of a target would lose their hidden status in my game, barring extraordinary circumstances. Their stealth allows them enough margin to get a shot off at an unexpected time or location.
So if the character is hidden behind a tree, leans around the tree and doesn't attack is he still hidden? No. So why does he get an attack from hidden after revealing himself?
Again the DM may allow an attack against a distracted enemy but it is not automatic.