The MAC address is used every time you send a packet, and there's no case where just the IP is used. Most traffic you'll see is either using
TCP or
UDP packets for transport. These are encapsulated in
IP packets, which are in turn (on a regular ethernet or WiFI network) encapsulated in
Ethernet frames. TCP and UDP know nothing about the network structure, and don't really care, because that's the job of IP. IP in turn doesn't know anything about the data it's transmitting, but only cares about getting it through the network. And Ethernet only cares about getting it through the wire to the next local point without colliding with another frame.
How do these parts play together? Lets say you want to establish a TCP connection between 2 hosts:
- | Host A | Host B |
Hostname | Hoth | Dagobah |
MAC Address | 00-11-22-33-44-55-66-77-88 | 01-23-45-67-89-ab-cd-ef |
IP Address | 10.1.2.5/16 | 192.168.5.99/24 |
Gateway | 10.1.255.254 | 192.168.5.1 |
I'm assuming here that both hosts already know the IP of the other (either by a static entry or through DNS).
A data packet from Hoth to Dagobah can't be sent directly, since they're on different subnets, so it has to go through the gateway. But IP doesn't allow for multiple destination addresses. If the packet is sent with the gateway IP as destination, it won't be sent out on the other network. And we can't get the MAC address of the other host, since that information isn't routed to us. The solution is that the packet is sent with the IP address of Dagobah, but with the MAC address of the gateway. That way the routing gateway has to look at the packet (it was directed there), sees that the real destination is on the other network, and sends it there.