I know that Muhammad encouraged the freeing of slaves throughout his lifetime, but I'm wondering why it wasn't eventually made forbidden to own slaves, like for example how alcohol was made forbidden?
Islam banned all forms of slavery except within the context of war where slavery serves as an alternative to a captive being killed when the ruler deems the individual unfit to be released or ransomed. It's a way to save the captive's life (the end goal still being the freeing of slaves). Outside of war, slavery is forbidden. Prophet Muhammad (ﷺ) said in hadith that on the Day of Judgement, he will be against "one who sells a free person (as a slave) and eats the price" ~ Bukhari 2227.
Slavery serving this purpose within war is the reason there is no total Qur'anic ban on slavery whereas, in contrast, there being no usefulness and mostly harms associated with alcohol is the reason there is a direct Qur'anic ban on alcohol.