A flat tire isn't necessarily the end of your ride.
A flat tire can turn a great ride into a bad one in no time flat. (Pun fully intended.) Unless you ride a Ural, motorcycles don't typically carry spare wheels. With a little luck and a little elbow grease, you can make your tubeless tire hold air again, at least for the ride home or to the shop to get replaced.
It's important to note that I specified tubeless tires. If your tire has a tube, like on bikes with spoked wheels such as cruisers and dual-sports, this method won't work, so don't even try it. There's still hope for you, though. You can either patch the hole in your tube or carry a new one that holds air and swap it over.
The most common cause of air leaks is running over debris that sticks into your tire, like nails or screws. If you're lucky, it will stick in there really well, resulting in the air leak being quite slow. This gives you time to notice the change in pressure from the bike's handling, or from frequent pressure checks, which all of us are doing, right? Anyway, in this case, you'll likely be doing this in the comfort of your own driveway. If you're unlucky, you might be doing it on the side of the road.
What you want to see is a puncture in the meaty part of your tread, like this. If the puncture is in the sidewall, forget about it. You need a new tire. Don't even try plugging the sidewall. It won't be safe, even if it holds air temporarily. This hole, however, is in the middle of the tread and can be plugged.
What you need is a basic plug kit like this. It consists of a rasp, a needle tool, five or more sticky strings, and maybe some rubber cement. Some sticky strings are already coated in adhesive, which is fine too. All that's not included is a pair of pliers to help remove the nail, screw, dinner fork, 10mm wrench, torpedo, etc. from the tire.
The next step is to make the hole bigger. Yes, bigger. This seems like the opposite of what you want to do, but making the hole a known size will help the plug fit inside it properly. That's what the rasp is for. Stick it into the hole in your tire and work it in and out until the hole is the same size as the rasp. This also makes the rubber on the edges of the hole quite rough, which will help hold the plug in place.
Now that the hole is prepared, it's time to plug it. Grab the needle tool, as well as one of the sticky strings. This string is your tire plug. Stick it through the hole in the needle tool about halfway, so the ends are even.
If you have rubber cement, glop a bit around the sides of the hole. Then shove the needle tool, plug and all, into the hole until about one-third of the string remains visible, and pull the needle tool out. The needle tool is split at the tip like a pinched fork, so you can remove it from the tire while leaving the plug behind.
Some people recommend cutting the string and rubber cement flush with the surface of the tire. You can certainly do that, but I've also found the excess string and rubber cement will wear off quickly from riding, leaving a similar result. Once the cement dries, reinflate the tire to your manufacturer-recommended pressure and check for leaks. More than likely, you won't have any.
Obviously, this means carrying some way to reinflate your tire with you. Some people rely on small tubes of CO2, while others carry a traditional electric tire pump. I prefer the electric pump. It may be bigger and heavier, but it'll never run out of air the way those small cartridges might if you haven't sealed everything correctly.
How safe is it to ride on a plugged tire? Obviously, the best solution is to replace it as soon as possible. The tire's structure is compromised, and although it now holds air, it was never meant to work with a hole in the middle of it. If you can't get the proper size tire quickly, though, or if you're simply flat broke, you can get away with riding gently on a plug for quite some time. I wouldn't recommend any track days or high-speed canyon runs on a plug, though.