The reasons why for Mac can't boot from a USB drive could be a poor connection, incompatible macOS installer, incorrect Startup Security Utility settings, drive corruption, etc., you can fix these possible causes to boot a Mac from a USB drive successfully.
Apple enables you to boot Mac from USB to upgrade/downgrade macOS, try new macOS, and for other reasons. With the bootable USB drive connected, you can select the startup disk by holding the Option/Alt key(Intel Mac) or power button(Apple silicon Mac) while restarting your Mac.
Mac won't boot from a USB is not a simple issue. If you only see the bootable HDD, no USB drive to choose from, and "the flash drive works without problems when the MacBook is loaded," then the connection should be in good condition, and the USB drive should not be corrupted. For an unbootable USB drive that performs abnormally, perhaps being damaged, you may need to recover data from it with iBoysoft Data Recovery for Mac and repair the drive with Disk Utility's First Aid and
Now, you can proceed to check the compatibility of the macOS installer, and the Startup Security Utility settings.
Is the macOS installed on the USB drive compatible with your Mac model? If not, you can't boot Mac from it. Check your Mac model from the Apple menu > About This Mac, and visit support.apple.com to see whether the macOS version you wanted supports your Mac model.
Then, check the Startup Security Settings on Intel Mac, perhaps, you don't allow booting from external media. You need to boot into macOS Recovery Mode to access Startup Security Settings from the menu bar, check Allow booting from external or removable media under Allowed Boot Media, and then try to boot the Mac from USB.