Abraham returned from the war against the kings, bringing back a great deal of booty.
The speaker in verse 20 is Melchizedek, and the "he" who gave Him tithes refers to Abraham, as Hebrews 7 clearly states. This occurred around 430 years prior to the making of the Old Covenant. Tithing is not stated here as a law but is introduced into the flow of the story of the Bible as an already ongoing practice, which Abraham already knew. How did Abraham know to give ten percent?
How did Abraham know God's laws, which were formally written 430 years later? By God's own testimony, Abraham kept them and was faithful (Genesis 26:5). There are two possible answers.
First, in James 2:23, Abraham is called "the friend of God," indicating a close relationship. He is the only one in the Book who is called God's friend. In John 15:14, Jesus said to the apostles, "You are My friends. And, do you know what? Because you are My friends, I am going to tell you what I am going to do."
God told Abraham His laws! God says Abraham heard and obeyed in Genesis 26:5. How did he know about tithing? God told him about it. Abraham was God's friend, and God wanted Abraham to act righteously. Because God did not want his life to be a mess, He instructed him in His way, His laws, and commandments!
Secondly, God told Adam and Eve His laws, being their Father. What kind of Parent would He be if He sent them out into life without instruction? That is a parent's responsibility, and God instructed His children.
Consider Genesis 4, in which Cain and Abel made their sacrifices. How did Cain and Abel know what to sacrifice? Did it just pop into their minds? Adam and Eve, who had walked with God in the Garden, told Cain and Abel what the appropriate sacrifices were. When the time came to sacrifice, Abel was obedient, while Cain was not. In Romans 4:15, Paul said that where there is no law there is no transgression. God spoke harshly to Cain, and pronounced a curse on him. If Cain did not know better, then God would have been unjust in His punishment.
Abraham knew God required tithes. If we follow tithing through the Bible, it does not even appear as a law until the book of Leviticus and Numbers 18 for the priesthood.
Next, Jesus Christ commands tithing in Matthew 23:23. Our Lord and Savior was in favor of tithing. He should be, because He gave it at the beginning. He told Abraham about it. He assigned it to the Levitical priesthood. Then, by very strong implication in Hebrews 7, tithing is assigned to the church. There has never been any deviation. Tithing has always been God's manner of financing His educational service.