Two ways to use an older iMac of any screen size and any year model earlier than the 2010 models. Only more modern ones can use Targeted Display Mode between two Mac OS computers (either two Apple machines or an iMac and a PC running Mac OS).
is to strip out all hardware from the old iMac body shell but the LCD panel and get a panel control board, and some cables in a kit and a 12VDC PSU for that conversion. Not expensive but takes care and some electronics talent. Search Google how and where to buy the kit on eBay or other places.
is to use the iMac as is, but with an operating MacOS hard disk running it. You will have to do that step first. Next is put both on same network. Ethernet will perform a bit better than wireless. Not expensive for a couple cables and a switch or hub. Even a peer to peer transfer ethernet cable (not a regular one) will do. Next search the Apple software suppliers for a program to run on both machines. One will be a server (the master that runs your programs, can be PC or iMac in some of these utilities) and the other will be the client (the target that will be the extended display of the other). iMacs, PCs and some Android tablets and phones can be used. Some programs are free, some have a trial period of a few minutes, then you buy, some are a few dollars. Another choice is a remote screen view utility like TeamViewer or other VNC that will let you see the PC on the iMac screen or vice versa. But this is not an extension for double the screen area like the others are.
Using computers since 1962. Running 7 older iMacs, 26 PCs and laptops for many flight simulators as wide extension screens for several decades, building, upgrading and repairing PCs and a few laptops for many years.