It refers to those who were not written in the book of life.
It is better read in Young's Literal Translation (YLT).
" And bow before it shall all who are dwelling upon the land, whose names have not been written in the scroll of the life of the Lamb slain from the foundation of the world;"
The English translations that use "earth" instead of "land" are making an assumption, and not allowing for the original use of "earth" or "land" from the OT. Whichever "land" or nation that God sent His prophet to warn was the "earth" or section / territory of the entire earth that was falling under God's judgment. So, using the word "earth" did not mean the whole entire earth, but just the land or nation that was under judgment.
The other word that confuses many is "heavens". Many times in prophesy it referred to the ruling authorities (the king, queen, princes, governors, etc.) of that land or nation, and their palaces or abodes. The "heavens" of the land (the principalities of Eph. 3:10) were over and above the people of that nation, and were a type of "heaven" exercising authority over their people.
We then always have to determine which nation / land the prophet was sent to warn of the coming judgment of God in order to know which "heavens" and which "earth" was affected.
For instance, in Isa. 13:1 Babylon was the nation under judgment. So when we read that section of prophesy we can then identify the "heavens" of Babylon as the king of Babylon and all of his rulers, and the entirety of his kingdom. The "earth" of Babylon was the land and / or people of Babylon.
Therefore, Isa. 13:5,
"They are coming in from a land afar off, From the end of the heavens, Jehovah and the instruments of His indignation, To destroy all the land." (YLT)
"from the end of heavens" was the borders of the kingdom of Babylon; and "to destroy all the land" only applied to the land of the nation of Babylon.
And, Isa. 1:10,
" For the stars of the heavens, and their constellations, Cause not their light to shine, Darkened hath been the sun in its going out, And the moon causeth not its light to come forth." (YLT)
Was symbolic of the king (sun) and queen (moon) being removed from the power and authority they had held under their ruling "heavens" of Babylon. That their light would no longer shine was the prophetic language of being removed from power.
WE apply the same usage of prophetic language to Revelation. Who was the prophet, and who was He speaking to?
"A revelation of Jesus Christ, that God gave to him, to shew to his servants what things it behoveth to come to pass quickly; and he did signify [it], having sent through his messenger to his servant John," (YLT)
Christ was the prophet, the message was from God, and was shown to John. We remember that Christ was sent to "the lost sheep of the house of Israel" (Matt. 15:24).
"Lo, he doth come with the clouds, and see him shall every eye, even those who did pierce him, and wail because of him shall all the tribes of the land. Yes! Amen!" (YLT)
Those who pierced Him lived during the first century A.D. The tribes of the land referred to the land of Judea and Palestine where Christ and His apostles had been preaching the gospel of the new covenant.
So, Revelation was the prophesy of the judgment that was to come upon the wicked of the land of Palestine and Judea for crucifying the Messiah and for persecuting His saints. Therefore the "heavens" in this prophesy were the ruling authorities over the "land" or "earth" of Judea and Palestine.
In Rev. 13:8 all those who bowed down and worshiped the "sea beast" - the predator ruling power / government from the pagan nations that surrounded Judea which was the Roman empire - were all those who worshiped the Caesars of Rome who claimed to be gods. And, they were not written in the book of life of the Lamb (Christ) who was slain from the foundation of the world.
And, in Rev. 13:10, the ones who were making war against the saints (vs. 7) were those of the ruling authority (Romans) who led the saints into captivity, and who killed the saints with the sword. They would in turn be led themselves into captivity and be killed by the sword.
The code for understanding Revelation is the Old Testament. We have to find and understand the meanings and definitions of the prophetic language used in the OT before we can know how to read Revelation, or for that matter even the entire NT.
If you would like to read more about the sea beast and land beast of Rev. 13, see my post "The Signs of Revelation - Part V: The Animal Symbols of The Battle" here.