They do this mostly with spectroscopy, breaking out the light by frequency. The longer the light has been travelling, the more frequency shifts into redder frequencies, because the universe is continually expanding.

But those distances are all built on top of the cosmic distance ladder (Terence Tao explaining on 3blue1brown)

https://youtu.be/YdOXS_9_P4U?is=jbkFbwWKgbn3yOjq

Maps like DESI add the 3d distances.

https://www.desi.lbl.gov/education-outreach/interactive-visu...