I am friends with my ex-husband. He's even stayed in my house when he's come from out of state to visit our son. We met in 1977, dated for many years before we finally got married. After we married, we endured a tragic loss, the death of a son, which broke up the marriage. (The son we lost was his by another relationship, in case you're confused.)
I never spoke a bad word my ex to our son, as my ex struggled with drugs and alcohol after the loss. It took many years for him to get back on track. By then, our son was old enough to ask questions, which I answered truthfully, but in an age-appropriate way.
So, yes it can happen that you can remain friends with an ex, depending on the circumstances surrounding the break-up and how close you were during the relationship. Hope this helps.