Fashionable bags are not cheap, so if the zipper in a bag breaks, it makes sense to spend a few dollars on a zipper and replace it rather than to throw out the bag. If your bag is canvas, vinyl or even soft leather, you should be able to put the zipper back into the bag using a sewing machine. If it's made from a thick or heavy leather, don't attempt the repair. You'll likely ruin either your machine or your bag.
Measure the old zipper. Buy a replacement zipper of the same type and length.
Using a seam ripper, carefully break the stitches holding the old zipper to the bag. Note how the zipper is positioned in the bag. If the bag is lined, the fabric sides of the zipper will be sandwiched between the bag and the lining. If it is not lined, the zipper will lie under a folded-over section of fabric.
If the bag is cloth, pin the zipper face up in the same manner and position as the original zipper. If the bag is vinyl or leather, position the zipper as for a cloth bag, but use double-sided tape to hold the zipper in place. Pins will make holes in a leather or vinyl bag that will not disappear.
Open the zipper all the way. Holding the bag open, sew one side of the zipper in place, following the impression of the thread line left by the old zipper. Run the sewing machine in reverse for a few stitches at the beginning and end of the seam to secure the ends and prevent the zipper from pulling out of the fabric. Sew the other side of the zipper in the same manner.
Long stitches and a very sharp needle work best for sewing on vinyl and leather.