The only way that could happen is if you can upgrade the GPU card (which has the newer connector for the 5K display). Older 27" iMac all the way back to 2009 have been upgraded to new GPUs over the years. But with the newer form factor, I don't know how upgradable the GPU card is and the the 5K display having the exact same mounting screws as in the 2.5K display may also not be the same.
Even worse, you won't even be able to drive a 5K thunderbolt display from your 2012 or 2013 27" iMac because it won't have Thunderbolt 3 (DisplayPort 1.3 spec). Though, I think ThunderBolt 2 should be able to drive a 5K display at a lower refresh rate of 30 Hz? But this is not very desirable.
Remember, a 5K display is the same resolution as FOUR 2.5K (2560x1440p) displays. The current iMacs can drive two of these current Thunderbolt displays but not four! (Actually they can support a little more - two 2560x1600 displays)