Re: But how?
On my phone so slightly condensed but, TCAS asks other aircraft to transmit their transponder signal. This gives the height information and the transponder serial number. There are two receiving antenna which use phase difference to get a bearing to the other aircraft (reasonably accurate...). The time between transmission and reception lets the system calculate range. What it's really interested in is the rate of change of that time which indicates if something is closing and how quickly. If they're at a similar height you start getting alerts. If you get into resolution advisories the serial number is used to decide who should climb.
So everything is relative to the aircraft because that's all that's needed.