Carry the right tools
Pack a spare tube sized for your wheels, two tire levers, a mini pump or CO2 inflator, and a multitool. A patch kit is useful as a backup. Keep them in a saddle bag so you always have them, even on short rides.
The HEB kit below includes a mini pump, levers and patches, and the ROCKBROS saddle bag holds everything under the seat. A small kit in one place saves a lot of searching at the side of the road.
Removing the wheel and tire
Shift into a small cog and open the brake if your bike uses rim brakes. Remove the wheel, then push the tire bead off the rim on one side. Use the levers carefully, one at a time, to lift the bead. Never pry the tire with a sharp tool, because it can cut the tube.
Pull the tube out, starting at the valve. Check the inside of the tire for a sharp object, such as a thorn or glass, and remove it with a rag before you fit the new tube.
Fitting the new tube
Inflate the new tube slightly so it keeps its shape, then tuck it inside the tire. Push the tire bead back onto the rim, working from the valve. Finish with the last section using your hands, not the levers, so you do not pinch the tube.
Check that the tube is not trapped under the bead, then inflate to the pressure printed on the tire sidewall. Spin the wheel and look for uneven spots before you refit it to the bike.
When to repair the old tube instead
If the hole is small and you have a patch kit, you can patch the old tube at home. Let the glue dry fully before you press the patch on. A tube that fails repeatedly often has a hidden cause, such as a rim tape problem or a tire that has worn thin. Check the tire carefully when you get home.