The outcome of the shot is determined the instant you hit the ball.
So if before hitting the ball - you removed the flagstick - that is how the outcome is determined. ball cannot hit flagstick.
If you did not - then the ball may hit the flagstick. Most of the top players agree that hitting the flagstick gives you more bad outcomes than good - so removing the flagstick if you think you are going to get close is the best option.
The only problem is after removing the flagstick - you can't easily see where the hole is - so you are almost blind to how close the ball is going to the hole, which is nice to see.
As explained above - as the outcome is already determined - putting the flagstick back in after playing the shot - cannot possibly influence the outcome. We know that the ball cannot possibly hit the flag (as it really wasn't there when the outcome was calculated). So you are able to replace the flag in order to see where the hole is during the flight of the ball - without there being any risk that the ball will bounce off the flagstick and away off the green.
This is why you see the flag removed - then the stroke taken - then the flag replaced.