There's quite a lot of leeway.
If the tube says it's narrower than the tyre (like
@nickyboy's 18-23 tubes in a 25 tyre), it will just stretch to fit.
If it's a smaller diameter (like a 650c tube in a 700c tyre) or a couple of sizes bigger than the tyre (35-40 tube in a 28 tyre), it will be difficult to fit without it getting trapped under the bead, which can allow it to burst out as you pump the tyre.
Smaller tubes are lighter, probably very slightly faster, and take up less space in your saddle pack, but if you do puncture, the tyre will go flat pretty quickly. If you don't stop before the tyre is completely flat, you can get a snakebite in addition to the original puncture, which makes both realising that there is an original puncture, and finding the cause of it, more difficult. If you don't find the cause, the chances are good that you'll have another puncture in a few miles.
Larger tubes are heavier, bulkier, probably a bit slower, but if you puncture the unstretched tube will often go flat slower (giving you chance to stop before it's completely flat), and sometimes (with thorns, usually) it leaks slowly enough that you can ride home without doing more than pumping a bit.
I did once do 4 or 5 80-100 mile club runs with half an inch of thorn inside my inner tube, before I got fed up with having to pump up a soft tyre every 2 or 3 days, and found it.