Right after you feel that your bike’s tire is flat, you should stop riding and get off the cycle. You can then turn your bike upside-down in a way that it should balance itself on its seat and the handlebars.
Now you can remove the wheel with the flat tire. In order to do this, you will have to first loosen the brakes and then check to see how the wheel is connected to the bike. If there is a mechanism or quick release nut, you will have to loosen it to help the wheel slide out.
Loosen the tire from the rim. Slip the tire lever between the rim of the wheel and the lip of the tire, push the lever down to lift the tire from the rim and slide the lever around the wheel to remove one edge of the tire from the rim.
Push the valve stem out of the rim through the hole in the wheel and pull the old flat tube from the tire. Inspect the tube for tears or punctures and feel with your fingertips along the inside of the tire to look for thorns or glass that may have caused the flat.
Put the new tube into the tire by first putting the new valve into the hole of the rim and then tucking the tube under the tire.
Set the tire back snugly on the rim of the wheel, using your thumbs and pressing the edge of the tire back into the wheel. Make sure not to pinch the tube between the tire and the rim.
Inflate the tube with the bike pump or CO2 cartridge. Test the tire pressure by pressing with the thumbs and place the wheel back onto the bike and ride.