Q:

How does a car with keyless entry know where the key is?

If I hold the car key and touch the handle on the door, the car locks the doors. But if I leave the key inside the car, it won't lock itself.
How does the car know where the key is located? What mechanism does it use to triangulate the key? It can't be using ultrasound since ultrasound doesn't go through objects and is directional. It can't be radio wave either, unless the car can accurately measure down to nanoseconds.

A:

They either use multiple antennas for each exterior zone of the car and/or use RF in combination with other sensors, for instance detecting that the user has touched the front driver's side door handle.  
An Atmel brochure about these "passive entry/passive start" systems describes the latter:

Passive Entry (PE)
  The passive entry function allows the driver to unlock the
  vehicle's doors without activating the key fob. However,
  some user action is needed to trigger the system such as
  approaching the door, or touching or pulling on the door
  handle. When the vehicle detects such an activity, it starts to
  search for the key fob outside the vehicle cabin. This is called
  localization. Once the fob has been authenticated, the doors
  unlock automatically

It includes this diagram of a typical system: 
The former system, consisting of multiple exterior antennas is described here by a manufacturer of magnetic components:

PKE Systems work by having a series of LF (low frequency, 20kHz, 125kHz & 134kHz) transmitter antennas, depending on chip-set used, both inside and outside the vehicle. External antennas are located in the door hadler, mirrors or trunk position. When vehicle is triggered either, close to vehicle, pulling the hadle or touching it an LF signal is transmitted from the antennas to the Key. Key is activated and transmited its ID back to the vehicle using RF channel, if Key code is correct the electronic module unlock the vehicle.

They include the following diagram:

In order to prevent the key from being locked in the car, both systems also include a set of antennas inside the car that are designed specifically to detect keyfobs that are specifically inside of the car.  These, as well as the exterior localized antennas use lower RF frequencies presumably to improve localization of the signal.  The ability of the interior antennas to discriminate keyfobs inside versus outside of the car is further helped by the car's shell being a large metal box that will help to impede the antennas from picking up keyfobs outside of the vehicle.

