What is the latest 27" iMac model which can be used as an external display on a MaBook Pro 15" Mid 2015?
And can I run latest macOS on both MBP and iMac to have this working or need to use older macOS?
Ask Different is a question and answer site for power users of Apple hardware and software. It only takes a minute to sign up.Sign up to join this community
From Apple KB: Use your iMac as a display
Target Display Mode isn’t supported on iMac models with Retina display. Only iMac (27-inch, Late 2009), iMac (27-inch, Mid 2010), and iMac (Mid 2011 to Mid 2014) support Target Display Mode.
That's it, just those iMacs, so Mid 2014 would be the latest.
After comments: It seems you additionally need to be on High Sierra or lower, support has been dropped in recent OSes - From Apple KB: Use your iMac as a display with target display mode
Make sure that your iMac is using macOS High Sierra 10.13.6 or earlier. You can't use target display mode with later versions of macOS, or with Boot Camp and Windows.