How does a faucet work? How can I create one?

I'm a developer.

How do faucets on testnets out there work? How can I build one by myself?

That is, how would I generate USDC, DAI, USDT, <any_other_token>... or ETH, MATIC, AVAX.... on a testnet myself?

Faucets don't generate tokens. They pool existing tokens and distribute them in some way.