If you need to fix a flat tire, the first thing to do is remove the wheel. This article and video teach you how to remove a rear wheel that has a quick-release axle. (If you have a thru-axle, see the how-to details here.)
Video: How to Remove a Rear Wheel that has a Quick Release Axle
Removing the Wheel
First, put in your bike in a repair stand and shift the chain to the smallest cog. If you're in the middle of your ride or you don't have a bike stand, you can flip the bike upside down to work on it. Shifting to the smallest cog will help the wheel slide out more easily.
If your derailleur has a clutch that increases tension on the chain, turn it off. If your bike has caliper or cantilever brakes, open them so they don't catch the tire as you remove it.
Next, support the rear wheel and loosen the quick-release lever on the axle. Spin the lever counterclockwise until the wheel is loose in the dropouts.
To get the wheel out smoothly, pull down on the tensioning arm and back on the body of the body of the derailleur. The wheel should slide right out.
Reinstalling the Wheel
For this next part, take the bike off the stand. Get the derailleur out of the way by pulling it down and back. To help the wheel slide in more smoothly, make sure one of the smallest cogs engages with the upper section of chain.
Push down gently on the saddle and make sure the axle is securely seated in the dropouts. If the wheel doesn't slide in easily, remove it and try again.
Hold the quick-release lever in place and tighten the bolt on the drive side of the bike. Once the bolt is tight, close the lever. Make sure it's tucked out of the way and not touching the bike's frame.
If the lever closes too easily and is touching the frame, it's probably not tight enough. Open the lever and tighten the bolt some more. Then check for proper tightness.
If you have caliper or cantilever brakes, reconnect them and make sure they're working.
If your derailleur has a clutch, turn it back on.
Finish by spinning the pedals and making sure the gears shift normally.