People like William Wilberforce campaigned in parliament to make it illegal to buy and sell people as slaves and force them to work for free. We opened our mind and decided it is wrong to use people as slaves, all people should have the same rights. When we abolished the slave trade, all nations that were part of the empire inherited the law and automatically declared the trade of slaves as piracy. Then we used our huge navy to intercept slave boats from nations that weren't a part of the British Empire so we blocked slave ships from entering the ports of Brazil.
In the 17th century, every nation on Earth had a significant slave population, it wasn't just the Portuguese dominated Atlantic slave trade, it was the middle east, Asia and many African nations had their own slave trade. Britain abolished all of that using democracy and our powerful navy.