You're asking about the OSI Network Model, even though you don't realize it, and even though the OSI model doesn't quite relate to TCP/IP.
The way computer networking works is in layers. The layers in the OSI model are:
- Physical (e.g., copper, fiber, etc.)
- Data link (e.g., ethernet, ppp, fddi, etc.)
- Network (e.g., IPv4, IPv6, IPX, etc.)
- Transport (e.g., TCP, UDP, etc.)
- Session (Used in OSI, but most things here are handled by layer 4 in TCP/IP)
- Presentation (e.g., ASCII vs. EBCDIC, or MIME)
- Application (e.g., HTTP, SMTP, DNS, etc.)
In order for two computers to communicate over a network they must be speaking the same network layer protocol. It doesn't have to be IPv4 or IPv6, it could be IPX or X.25. But it must be something.
We'll just assume IPv4, since that's most common.
Both computers will need an IP address. In this day and age most likely they will be assigned one from a DHCP server or will dynamically assign their own using ZeroConf. So they will have IP addresses.
What happens when you ping?
First the computer initiating the session will send a broadcast layer 2 ARP packet. ARP is Address Resolution Protocol. ARP is used to discover the layer 2 address associated with a particular layer 3 address. A broadcast is a special packet that instructs the network switch to send the packet to all physical ports with a connected link (i.e., with L1 connectivity). The computer that has the indicated Layer 3 address will respond with a unicast ARP reply. ARP happens entirely over L2 addressing (ARP results are cached for later lookup so it doesn't have to keep sending ARP requests).
Next an ICMP echo-request packet is crafted. The source L2 address will be the originating computer's Ether address. The L3 source will be the originating computer's IP address. The destination L2 and L3 address will be those discovered from the ARP. The packet is then sent over the wire to the switch.
The switch examines the destination L2 address and looks up in it's CAM table to identify which physical port has the specified Ether address. The packet is then sent over the wire on the correct physical port.
When the destination computer receives it, it first examines the L2 address to make sure that it matches the L2 address of the physical interface that received the packet. If it matches then it checks the L3 address to make sure that it matches a Layer 3 address assigned to the physical interface. If all of these check out then the packet will be handled by the upper layer protocols (in this case ICMP, handled by the kernel). If not, the packet is dropped.
For the ICMP reply things work in pretty much the same way, except that the responding computer doesn't need to send an ARP request, since it received a packet with an L2/L3 pair it can install that directly to the ARP cache instead.