STUN is a single-purpose protocol: a peer sends a STUN request to a public server, and the server replies with whatever IP address and port the request came from. That reflected address is the peer's public mapping, and it's the one the other peer needs to know in order to connect.
STUN solves the most common case — a peer behind a single, well-behaved NAT — at almost no cost. The server doesn't carry media, it doesn't carry signaling, it just answers a small UDP packet with a small UDP packet. Public STUN servers run by Google and Cloudflare are free to use, which is why almost every WebRTC tutorial assumes you have one.
What STUN doesn't solve is symmetric NAT, where the public mapping changes depending on the destination. Two peers behind symmetric NATs can't connect directly even after STUN, which is why every production WebRTC stack also configures a TURN server as a fallback.