For the most part they’re simply timed. For timed stoplights a lot of different methods that fall under the field or transport engineering are used to determine what the expected traffic load will be at any given street, and traffic lights were timed accordingly. This means that not all stoplights have the same duration to prioritise flow of traffic and optimise it.
However since this isn’t a perfect system because usually the timing is on a fixed loop which may not account for variable traffic conditions. This is why stoplight sensors are also used on many stoplights. The most common ones are induction loops, that being wires embedded in the asphalt that can detect when a car is waiting at the stoplight. However in higher traffic areas, radar, camera or other types of sensors are employed to calculate the overall number of cars on the road to try to adjust stoplights more dynamically and help optimise traffic flow in times of heavy traffic. Stoplight sensors are also handy in places with very little traffic, where usually there’s a main road with most of the area’s traffic passing through and the roads connecting to it have comparatively very little traffic. Stoplights on those connecting roads will have sensors and will only turn green if they detect a car waiting at the stoplight, otherwise they remain red so that the main road maintains a steady traffic flow. This is a better alternative to not having stoplights at all because even if those roads are low traffic that’s exactly the kinds of conditions that make people not pay as much attention as they should or going over the speed limit.
Latest Answers