Replacing a toilet flange is a somewhat complicated, and the process depends on the type and size of the pipe.
The job is a little easier if you have a 4-inch inside diameter pipe. You need to purchase an inside closet flange. This type of flange either expands inside the pipe or can be glued inside the pipe. If the pipe is plastic, you can leave the old flange in place while you glue on the new flange at the appropriate elevation for the floor. You want the bottom edge of the flange to be flush with the floor. You may need to cut away the old flange if the new one does not sit flush.
If you have a 3-inch inside diameter plastic pipe, the old flange will have to be removed from the outside of the pipe. This can be challenging since you have to make the opening around the flange large enough so that you can peel it away without damaging the pipe. It often takes professional plumbers a number of years to master this technique, so this may not be something that you want to undertake yourself.
If your flange is cast iron, you will need a hammer and chisel to break the flange so that it can be removed. If you have 4-inch diameter pipes, you will need an inside closet flange with a rubber gasket that will expand and create a seal on the inside of the pipe. In order to create a good seal, you will need to remove any buildup on the inside of the pipe. You can do this by sanding or scraping using a drill with a wire brush. Be sure to use a little silicon on the gasket as you start to tighten it. If you have a 3-inch cast iron pipe, you will need to use a hammer and chisel to remove the old flange from the outside of the pipe so that you can install the new one. If the flange has a rubber gasket, you will need to remove the gasket with a sharp knife and replace it along with the flange.
If you have a lead closet flange installed on the outside of the pipe, you will start by removing the old flange and recaulking it. You then need to pour a new lead joint. Another option is to install an expanding flange on the outside, but this would entail breaking up the floor in order to set the flange into the floor.
Every flange must be screwed into the floor so that it does not rise as you tighten down the toilet. You do this by inserting wood or concrete anchors through the holes in the flange. It is important that the anchors are brass or galvanized to keep them from rusting and breaking. Never tighten the flange with more than a 6-inch crescent with two fingers on the handle. Plastic flanges should have a stainless outer ring to provide extra strength and to prevent rusting. Set the toilet so that it is level, and be careful that you do not tighten it so tight that you break the flange. The final step is to seal the edge along the floor with caulk to prevent odors.