Some traffic lights don't operate periodically but instead detect when a car is close by and then turns green. I have heard that they use a magnetic sensor embedded in the road to sense cars as they come near. Is this correct? Do they use other means as well?
As others stated before, induction loops are the primary - most reliable method: the coils (usually just several loops of wire) embedded in the road; fed given frequency from a generator, in presence of metal the frequency of the LC circuit changes and the sensor circuitry detects the change of frequency, producing a presence signal. In some cases these may fail to detect bicycles, but they are by far most common as they aren't affected by weather (or more precisely, the detection circuit tunes in to slow changes of frequency caused by weather) and are immune to accidental false positives. Note the loops can be localized (~2m size) or cover a lengthy part of a lane.
Detection is performed by cards like these:
and by induction loops made with wire laid in grooves like these:
or placed in pipes under the road surface at construction time (in the photo is a loop for tram detection, but pre-built loops are similar)
Videodetection - cameras connecting to a specialized card with "detection zones" defined through specialized software detect the vehicles. They are vulnerable to bad weather and tend to produce false positives from glare of car headlights, shadows of vehicles on neighbor lane and such, but in certain cases - primarily where road surface makes installing detection loops impossible (gravel, or bad road surface) they are preferred. Additionally, the video detection cards are significantly more expensive than cards for detection loops.
There are a few lesser used techniques like geomagnetic (detecting changes in magnetic field; These largely depend on size of the vehicle, so a large truck can trigger a sensor in neighbor lane - but they are more durable), radar (detect only moving vehicles* - but are frequently used to detect pedestrians as they rarely stay immobile), laser (measuring distance to road surface; vehicle in the way changes the distance measured. Quite reliable but only point-detection, no area detection).
Pictured below is a geomagnetic sensor:
and a radar sensors (short range for pedestrians and bicycles, and long range, for cars):
I heard of pneumatic and piezzoelectric, but I've never seen these in use for traffic control - probably problems of wear and durability; I know these are used for automated barriers for parking lots, but they obviously support an order of magnitude lower traffic.
For city transport traffic, the vehicles are equipped with an on-board computer with a short-range radio (up to 500m) and GPS, and they broadcast messages about entering pre-defined "checkpoints" to the traffic system, alongside with data about intended turn direction, delay against schedule and some others, allowing the controller to prioritize. An alternative is a system that feeds vehicle position to a central unit, which then contacts controllers with messages about prioritizing these vehicles.
Last but not least, cameras/sensors detecting strobe lights of specific frequency give immediate priority to oncoming emergency vehicles. (and take a photo of the vehicle in question, to prevent abuse.)
Controllers can communicate with each other, and share their detector states, so two controllers can use each other's detectors, for example when they are a short way away from each other.
Two induction loops in a short distance (~1m) from each other are used to determine speed and length of vehicles, making adapting to longer or slower vehicles possible. Another application of pairs of detection loops near to each other is in directional detectors - basing on the order the neighbor loops are activated one can determine the direction the vehicle is moving. This is rarely used for cars but if a single rail line with trams (street cars) moving in both directions crosses a road, the same two pair of detectors can activate the green light for the vehicle and then register it finished crossing the street, regardless of its direction as the pairs can generate "approaching / departing" signals.
A special "virtual" detector composed of two loops in one lane in a considerable distanc measures the length of queue of cars, allowing prediction of time necessary to vacate the lane (and making "time countdown displays" viable.)
Another special type of detector is a "blocking" one, placed either in the middle of the crossing (camera) or behind it, on the "departing" lane (usually a detection loop); its purpose is to delay/block entry until the crossing is vacated, or prevent blocking the crossing if a traffic jam formed in the "exit" lane and new vehicles would be unable to depart.
Note this is the "standard" set, but since the controllers can accept a standarized 24V/'contact' signal, any generic source can be used, for example an infrared remote control to enable that one specific direction which is used in 0.1% cases, activated by the owner of the house with driveway right into the crossing, or by a manual trigger from a factory gate to enable a truck to enter/leave, or whatever need arises.
Below is a generic 16 inputs/16 outputs card. These are usually used for pedestrian buttons (and lamps) but they can provide signal from arbitrary sources and control arbitrary end-point devices.
In some cities detectors work in "pairs" of two types; for example detection loops are very reliable for detecting vehicles, but mechanical stress from heavy transport can damage them, and repairing them is not a trivial matter. The card can detect a damaged loop (usually open circuit -> no frequency or short circuit -> very high frequency) and in such case the controller starts using a backup sensor, for example radar or laser.
And just a screenshot from one of the controllers showing the map with detectors displaying their state live (blue = active). Note that detector on the far right - it doesn't belong to this controller; it's composite data from a neighbor controller, so that the short road connecting the two doesn't get congested - as long as there are cars waiting in the potential congestion zone no more will be allowed into it from the other directions.
*Note that while radar detectors can only detect cars in motion, that doesn't mean they can't be used as a standalone solution ("just support"). Sometimes the induction loops are placed at wrong locations as well (for various reasons, incompetence of the investor not the least of them), so cars stop behind/between them and don't trigger them during red light. This is still not a very big problem as any detector can be set as one with "memory". Any vehicle even momentarily activating such a detector cause it to keep the active state until green light on the associated lane, then act as normal ("forgetful") during the green light. Also note this is the default behavior for pedestrian pushbuttons.
Of course this is not ideal, as a vehicle may get stuck right out of the detection zone exactly during the change from green to red, or (say, due to driver's fault) miss the whole green cycle altogether. Still, these are relatively rare cases, especially that another approaching vehicle will usually trigger the detector anyway.
There are two techniques commonly in use.
The first is induction loops cut into the road surface. The metal in a vehicle body induces a current in the loop, as the vehicle passes over it.
(public domain pic from wikipedia)
The other is cameras / radar (either Doppler microwave for moving vehicles, or infra-red for static vehicles at the stop line), typically mounted on top of the traffic light:
(pic from the archived UK DfT site)
There are other techniques, much rarer, that might include visible-light camera for special circumstances; and pneumatic loops for temporary lights. Oh, and someone somewhere has probably used piezo-electric sensors too.
The induction loop is the most reliable for switching on demand. Though it has the down side that motorcycles are more difficult to detect because they have less metal to detect. a similar issue exists for carbon fiber cars.
Radar is typically only used to extend the green times as it can only detect moving vehicles reliably.
Some intersections use a camera and image recognition to trigger virtual detectors. These have the advantage that it can deal with a truck unloading right at the intersection (ignoring it and moving the spot to detect to next to it.
Public transport vehicles (trams and buses) can be equipped with transponders that log in special loops which allows the intersection controller to know which way the bus needs to go and give it green earlier and extend the green beyond the normal limit (or give it green at all).