Using an iMac as a monitor
Using an older iMac as a display for another Mac sounds like a great idea, right? Apple thought so too, a while ago. Use your iMac as a display with target display mode - Apple Support describes how it was done.
The last iMac that was eligible to be used as an external monitor was discontinued October 2015. If your iMac is newer than that, it cannot be used as an external monitor. End of story.
If you have an eligible iMac, read on. That Apple Support document describes what to do. There are many requirements. For example, the iMac to be used as a display must be running High Sierra or earlier, so if that's not already installed on that iMac, downloading High Sierra has become problematic for a while now. Workarounds exist, and are commonly described on this site if you search for them.
Next: The Mac that you would like to connect to the "display" iMac also needs to be "old" (2019 and earlier models qualify) and must also be running a commensurately outdated version of macOS.
It ought to be abundantly clear by now most Mac users will quickly lose interest in using their old iMac as a display.
You can use nearly any Mac to share the screen of another Mac: Share the screen of another Mac - Apple Support, but Screen Sharing is no substitute for an external display. Connect one or more external displays with your Mac describes how to connect them.
For additional alternatives, potential third party solutions and other suggestions, read Can you make a Mac a second display to another - Apple Community.
Also: About Target display mode on an iMac - Apple Community
Summary: If you need an external monitor, buy one. Target Display Mode is a thing of the very distant past.