In a nutshell
A semaphore is a gatekeeper with a count of available passes. To enter a shared section a thread asks for a pass (wait); if none is free it lines up and waits. On leaving it returns the pass (signal) and the next in line is let through. With a single pass — a binary semaphore, or mutex — exactly one thread touches the shared data at a time, so their updates can't overwrite each other.