First thing every host (host A and B here) has their routing table and arp cache table.
When host A tries to reach host B , host A will see it's routing table first to look for which interface it should send traffic out. Assume host B is in the same network as host A here.
We already know host B IP , now after routing table host A will see arp cache to see if it has host B MAC address already cached or not. In this case we suppose its not there.
Now, what host A will do it will fill ethernet frame(layer 2) destination MAC as something like FF:FF:FF:FF:FF:FF:FF (which means broadcast aka ARP). ARP(Address Resolution Protocol) is layer 2 protocol. Remember till now layer 3 protocol type (Internet Protocol) is not applied or we can say its padded.
ARP packet contains following fields (you can also see this from wireshark pcaps just filter it out for arp):
Destination MAC: FF:FF:FF:FF:FF:FF:FF --> In pcaps you will see it like
"Broadcast" inside wireshark
Destination IP: host B
Source MAC : host A
Source IP : host A
Of-coure source IP and destination IP are coming from layer 3 only here but no layer 3 protocol is in use till now as connection between host A and B not established yet. Once connection will be established then only any layer 3 protocol will be in use.
Here , how switch will learn about host A MAC address for the first time and it will update this in its MAC table if it dont have entry for it earlier.
Now, Once switch sees Ethernet frame destination MAC as broadcast(ARP), it will send ARP request to every host within that network. Remember as ARP is layer 2 protocol it will not be routable to other networks other than it is sent from i.e network of host A here.
Till now ARP will reach out to every host within that network, asking who-so ever has please tell your MAC address to host A. Once ARP reply is received from host B as destination IP is of host B in ARP packet, only host B will respond other will simply discard it,
Here, switch and host A both will update their MAC table and arp cache table respectively for host B's MAC address.
This is how Switch learn its MAC table.
Now, coming to if host C also tries to reach out host A. Same process will follow and only host C will respond with its MAC address others will discard as destination IP will be of host C only inside ARP packet. Assuming neither switch nor host A has MAC info available or we can say host A --> B and host C --> A took place at same time.