The toilet flange has two important purposes: The first is to hold the toilet to the floor, and the second is to provide a seal between the bottom of the toilet and the waste line. If the flange fails at either of these functions, it can't perform the other one and may need to be replaced. Before you do that, though, consider repairing the flange, which is much easier than replacing it.
Symptoms of a Bad Flange
The flange has a metal or plastic ring that you screw to the floor, and this rings has notches for bolts that fit through the bottom of the toilet to hold it down. Metal flanges are the ones that usually fail, and they do so because the humidity under the toilet corrodes them. When this happens, the toilet becomes unstable, and it may rock when someone sits on it. Each movement disturbs the wax ring that seals the inside of the flange, and when this ring fails, you'll probably see water on the floor and smell sewer gases. Both are conditions that call for immediate attention.
Removing the Old Flange
You need to pull up the toilet to access the flange, and when the toilet is off, any damage to the flange ring is immediately evident. If you can't repair it with a repair ring -- and flange replacement is the only option -- start by unscrewing the flange from the floor. Once all the bolts are out, you need to cut the flange off the waste pipe, and the best way to do this depends on the plumbing specifications. If the flange is connected to a closet bend, you need to remove part of the floor or go underneath it and cut the bend off the horizontal pipe. If the flange connects to a vertical pipe, cut the pipe at a place that allows you to easily glue on a coupling and a length of new pipe.
Preparing for a New Flange
When you remove the old flange, you may also have to repair the subfloor, which likely got soaked and rotten because of the toilet leak. After making those repairs, put the flange temporarily in place and ensure the top of the ring comes to the level of the finished floor, or less than 1/4 inch below it. If it does not, you may have to add plywood to the subfloor or cut back some of the floor covering. Once you've ascertained that the level of the flange with respect to the finished floor is correct, you can install it.
Installing a Flange into PVC Pipe
Install a new closet elbow or length of vertical pipe that comes flush with the top surface of the subfloor before you install the flange. Dry-fit the flange and orient it so that a line through flange tracks would be parallel to the back wall. Then drill pilot holes for the screws in the subfloor and remove the flange. Spread solvent-weld glue on the outside of the flange pipe and the inside of the waste pipe, and then push the flange into place and turn it quickly to line up the pilot holes. Drive screws into the holes to secure the flange to the floor, and it's ready for the wax ring and the toilet.
Dealing with Cast Iron Pipes
If you live in an older house, your waste pipes may be cast iron, and removing the flange will involve cutting these pipes with a hacksaw. The best way to prepare the pipes for a new flange is to cut them back far enough so you can connect plastic pipe with a rubber coupling, and then run the plastic pipe to the toilet opening and connect the flange.