It looks like the last iMacs with (Thunderbolt) Target Display Mode came out int 2014, but the Retina (and later) models don't support it unfortunately:
https://www.lifewire.com/use-imac-as-monitor-with-target-dis...
No reason Apple can't bring it back - it greatly increases the value and lifespan of an iMac!!