27" Late 2009 iMacs don't have Thunderbolt. They have Mini DisplayPort – which is an advantage when it comes to Target Display Mode, because it means you don't need Thunderbolt input, just regular Mini DisplayPort input. If that was the end of the story, all you'd need is a USB-C to Mini DisplayPort adapter cable - the same type of adapter you might use to connect to a modern standalone monitor.
Unfortunately, Apple put restrictions on the other device after the fact. It now must be a Mac that was introduced in 2019 or earlier and that is running macOS Catalina or earlier. Both restrictions lock out your M1 iMac. I suppose you could try using a USB-C to Mini DisplayPort cable and TDM, but there's no guarantee that it would work. (If it did, a future system update might break it out from under you.). Most likely result is you'll wind up with a spare cable that you or someone else could use to connect to a real display.
Use your iMac as a display with target display mode - Apple Support
People with iMacs made between 2011 and mid 2014 are in a much worse boat. Those Macs have Thunderbolt, and require Thunderbolt input (probably for technical reasons). Connecting a modern Mac with a Thunderbolt 3 / 4 port (USB-C connector) to an ancient one with a Thunderbolt 1 / 2 port (Mini DisplayPort connector) would set you back about $80 for specialized Thunderbolt hardware ($50 TB3-to-2 adapter + $30 TB1/2 cable).
Since none of the iMacs that support TDM have resolutions higher than 2560x1440 pixels, and monitors have come down in price a lot, spending $80 on specialized Thunderbolt hardware to try to use an ancient TDM-equipped iMac in a now-unsupported way isn't particularly attractive. Not compared to putting the $80 towards a new monitor that has the same or better size/resolution, and that probably has more years of life left before it fails.