Most UM Development boards have an IO connected to a voltage divider on the 5V power rail that you can read to determine if there is a 5V power source connected or not. This allows you to alter your code based on the source powering your board in your project.
If your board is being powered from the USB connection, this will of course also determine if USB is present 🙂
Note: Check the helper library for your board for a function that lets you read the state.
Note: The FeatherS2, TinyPICO Nano and NanoS3 do not include the VBUS sense capability. You can add this yourself by adding your own voltage divider connected from 5V to a free IO.
Detect USB Detect 5V