Two different clocks, two different fees. Part of freightcalcs.com.
Demurrage is charged by the port or terminal when your container sits there beyond its free time (commonly 3–7 days) — the box is occupying terminal ground. Detention (sometimes 'per diem') is charged by the ocean carrier when you keep their container out in the world beyond the allowed days — you've unloaded it at your warehouse but haven't returned the empty.
Both accrue daily and escalate in tiers; a slow week can cost more than the drayage itself. Avoidance is logistics hygiene: have customs paperwork ready before arrival, book drayage against the vessel ETA rather than after discharge, unload and return empties promptly, and negotiate extended free time into ocean contracts if your volumes justify it.