This is very simple, the padding is done in MAC layer (L2).
Consider IPv4 header as the responsible of getting the packet from one entity to another in an IP network. IP header contains field called "TotalLength" in 32bit word resolution.
the size received by the MAC is not important, so when a receiver wants to know the payload data size it base it's calculation on the IP header "TotalLength" field and similar fields in ip and higher layers (in TCP).
for example you have TCP above the IPv4, TCP has similar field that says where the data starts from the beginning of the TCP header again in 32bit resolution. if no TCP options are used than TCP header size is 20bytes
ActualDataSize = TotalLength - (IP_Header_Length*4) - (TcpDataOffset *4)
in UDP the header size is fixed so calculation is simpler.