In a cache like Redis or Memcached, you set TTL when you write a key, for example SET session:abc value EX 3600 stores it for 3600 seconds. Each key gets an expiry timestamp. Redis uses lazy expiration plus a background sampler: a key is removed when you try to read it after expiry, and a periodic job also samples random keys to clean them up so memory does not fill with dead entries.
In DNS the TTL is part of the record itself and travels with the answer. If example.com returns an A record with a TTL of 3600, every resolver and operating system along the path may cache that IP for one hour. Lowering the TTL to 60 before a planned IP change makes the switch propagate in about a minute instead of an hour, which is why teams drop DNS TTL ahead of a migration.
In an IP packet the field is different. IPv4 calls it TTL and IPv6 calls it Hop Limit. It starts at a value like 64 and every router that forwards the packet subtracts one. When it reaches zero the router drops the packet and sends back an ICMP Time Exceeded message. The traceroute tool exploits this by sending packets with TTL 1, 2, 3 and reading the error from each hop to map the route.