Topic-based messaging in Firebase works by clients subscribing to topics. When a client subscribes, their token is stored linked to that topic. The server sends messages to a topic, which are then delivered to all clients subscribed to that topic. This allows efficient broadcast messaging. The execution steps show subscription storage, message queuing, delivery to clients, and client reception. Variables track subscriptions and message queues. Key points include that messages only reach subscribed clients and unsubscribed clients do not receive messages. This method is useful for sending updates to groups of users without addressing each individually.