Try to find a display with DisplayPort input. 4k display will apply a heavy load to the weak GPU of your iMac. Better use 1080p display. IMac 2012 21.5" has only 1080p display.
You can connect the iMac to the external display with a miniDisplayPort to Displayport cable.
If you can only find a display with HDMI port, then find a miniDisplayPort to HDMI cable.
Even though the ports on the back of iMac 2012 were named Thunderbolt, they still can transmit video signal through DisplayPort protocol. MiniDisplayPort and Thunderbolt 1/2 share the same jack and socket.