There could be a variety of reasons why this may happen.
You're still "emotionally invested" in them.
Sex was the "best part" of your relationship.
It's lazy or easy/convenient as picking low hanging fruit
It may be an "ego exercise" to prove that you still can.
In some instances people hope having sex will rekindle the relationship in full.
Most recently I saw a young lady on TV state she has sex with exes when she is "in between relationships".
She said the purpose is to "keep (her number) of lovers down".
Essentially if she is "in the mood" she'd rather have sex with an ex than go to bed with some random stranger or a guy she is dating but is not yet comfortable enough with to have sex.
Her exes are human vibrators.