The background is that, after having been subjugated by the King of Elam for twelve years, five small kingdoms declared their independence, including the city state of Sodom.
The King of Elam prepared his revenge. Two years later, he and his three allies defeated the rebellious five kings. As Genesis 14:9 says, a war of four kings against five.
In the course of the war, the King of Elam captured Abraham’s nephew, Lot, who lived in Sodom. Perhaps he was going to sell Lot as a slave, perhaps he was going to hold him hostage.
But Abraham conferred with three other tribal chiefs and they went in pursuit. They had a small force but were triumphant (a familiar theme both in Scripture and in modern Israel!) He freed Lot and recaptured the people and booty taken by the Elamites.
Then Abraham is approached by the mysterious Melchizedek, King of Salem. The King offers a sacrifice of bread and wine to God, and blesses Abraham. Recognising the King as a priest of God and a precursor of the Messiah, Abraham gives him an offering, a tenth of his possessions.
Melchizedek was king of the city called Salem, now called Jerusalem. Reflecting on the Hebrew meanings of these words, Hebrews 7:2 tells us that says, the name 'Melchizedek' means 'king of righteousness'; then also, 'king of Salem' means 'king of peace.” And, as a ruler of peace, Melchizedek did not participate in the conflict between the nine kings.
For those interested in numerical patterns, he can be regarded as the tenth king, as the king who received a tenth of Abraham's possessions, and as a person reminding us of the ten commandments of God.
After his encounter with Melchizedek, Abraham is approached by another king. The king of Sodom asks Abraham to return the people taken from Sodom and suggests that Abraham keep the spoils taken from the city.
But Abraham refuses. He will not accept anything for himself (though he will accept spoils for his allies). In fact, he had taken a solemn vow that he would accept absolutely nothing.
Did Abraham realise the iniquity of Sodom? Is this why he refused to accept the king’s offer? Perhaps the passage is meant to contrast how we should venerate the King of Righteousness and have no part in the King of Unrighteousness?
If Abraham realised Sodom's iniquity, we can see an extra edge in his later bargaining with God on the city's behalf (Genesis 18). Perhaps he realizes the sinful nature of the city but still seeks God’s mercy for it. And, realising its iniquity, he seeks to expand God’s mercy proportionately.
(Until he comes to the requirement that there be ten just men. Numerically, this may remind us of the commandments and to stipulate the minimum compliance that a society needs to survive.)
Another point. We’d have expected that the king and people of Sodom people would be deeply grateful to Abraham, particularly those who had been taken as captives. So reflect on the viciousness of the people of Sodom when they later attack Lot, although they knew that he was the closest kinsman of their benefactor, Abraham (Genesis 19:9).
(By the way, I've referred to Abraham, but at this stage he was still called Abram. Later God gave him a new name to his part in Salvation History: see Genesis 17:5.)