Personally, I create my settings pretty much as required. I have a vague idea of what's going on, but I find that keeping my focus on the core group stops me from spending twenty pages outlining a city that I'll only be in for a few hours.
I'm with the consensus here, it's a bit of both. I knew my world, but some of it still had to bend as the story unfolded. For example I had a wall around the city. My critique group thought that wasn't very futuristic. So I had to modify it considerably.
Figure out the main points in your world, key locations, famous events and the like, and then fill in the rest as you go along.
One of my most popular fanfictions didn't have rules until I discovered them. (Video game personality sucked into a vodoo doll.) Even the MC didn't realize that bodily fluids were the controlling vehicle until late... she was just stitching them up and not realizing that her spit and hair were being part of the sewing project... and probably her blood considering the hand-work and clumsiness.
This. You have to know your basic, solid environment before you can grow on it. Think being in Williamsburg, Virginia in 1849 and you want to go to California. You know you need to head west. That's your basic plan. But, you don't know everything you are going to encounter on that journey west. THAT is what your characters will discover along the way. But you need to know enough of your world to be able to figure out what your characters might encounter or you, and they, will go nowhere.
I do it before, I love planning so it's fun and It's a lot easier to do it before- You're a lot clearer about things and you won't miss anything, for all you know you might have made some mistake or left a loop hole in a certain part of your world.
I agree with what others have said that the ideal is a mixture of planning and "just writing." I will add, though, that if you lean toward just writing you need to be willing to rip out large chunks of already written material as your concept of your world evolves. If your personality is such that you find that idea painful, you probably should be more of a planner.
I hit it right in the middle. I want to build my WORLD with a lot of detail, so I know how things work in it. However, my story itself, taking place in this world, can go to places I never knew existed. I try to fit the important bits together, and run with them from there. Then I do a chapter-by-chapter quick run down. This of course, always, ALWAYS gets modified, and new details are added to the world file. The two processes feed off of each other.