I know that the layer 2 doesn't understand IP, and the broadcast domain is same.
Why can't devices talk, how does it happen?
Devices in different subnets can communicate. That is the purpose of a router. Routers route packets between different networks.
Even if devices in different networks are on the same layer-2 broadcast domain, you need a router to let the devices communicate at layer-3. That is because each host will compare the destination layer-3 address and its own layer-3 address and mask to see if they are on the same network. If the destination host is on a different network, the host will send the packets in layer-2 frames to its configured gateway (router).
The host must assume that the destination network could be across the world from its network, and the gateway is the host on the network that knows how to forward packets toward the destination network.