Matthew 25:31-46 describes what has come to be called the "Judgment of Nations", which takes place when the Lord returns. It is commonly used to teach a works-based Salvation, as faith is not explicitly mentioned anywhere in the passage, whereas various good deeds are mentioned in context to a Heaven-or-Hell situation.
With that in mind, let's examine what this passage is teaching.
This judgment takes place when Christ returns to set up the Millennial Kingdom. The events preceding it are:
We know this because the passage begins with what is Christ dominating the entire world, where He is literally on a throne, with all nations gathered before Him:
Matthew 25:31-32
31 When the Son of man shall come in his glory, and all the holy angels with him, then shall he sit upon the throne of his glory:
32 And before him shall be gathered all nations: and he shall separate them one from another, as a shepherd divideth his sheep from the goats:
In the entire passage, there is no resurrection mentioned anywhere. This is a judgment on the people that are still alive at the end of God's wrath. The "rest of the dead" aren't raised and judged until over one thousand years later, after Christ has been ruling the entire planet for all that time (Revelation 20:4-6).
For a more complete understanding of the timing of this judgment in relation to various end times events, I recommend reading this article, which covers the topic in more depth.
The Lord then goes on to describe two possible outcomes of this judgment. All of mankind at this judgment is divided into two categories - "the sheep" and "the goats":
Matthew 25:32-34, 41, 46
32 And before him shall be gathered all nations: and he shall separate them one from another, as a shepherd divideth his sheep from the goats:
33 And he shall set the sheep on his right hand, but the goats on the left.
34 Then shall the King say unto them on his right hand, Come, ye blessed of my Father, inherit the kingdom prepared for you from the foundation of the world:
41 Then shall he say also unto them on the left hand, Depart from me, ye cursed, into everlasting fire, prepared for the devil and his angels:
46 And these shall go away into everlasting punishment: but the righteous into life eternal.
Above, we see that all of the sheep go to Heaven (verse 34, 46), and all of the goats go to Hell (verse 41, 46).
Based on this fact, establishing the identity of "the sheep" is easy, comparing Scripture with Scripture. These are believers.
We can look at other binary statements of Christ in Scripture, where all of mankind is divided into only one of two categories, to deduce this fact:
John 3:18
18 He that believeth on him is not condemned: but he that believeth not is condemned already, because he hath not believed in the name of the only begotten Son of God.
John 3:36
36 He that believeth on the Son hath everlasting life: and he that believeth not the Son shall not see life; but the wrath of God abideth on him.
1 John 5:10-13
10 He that believeth on the Son of God hath the witness in himself: he that believeth not God hath made him a liar; because he believeth not the record that God gave of his Son.
11 And this is the record, that God hath given to us eternal life, and this life is in his Son.
12 He that hath the Son hath life; and he that hath not the Son of God hath not life.
13 These things have I written unto you that believe on the name of the Son of God; that ye may know that ye have eternal life, and that ye may believe on the name of the Son of God.
The above verses, just like the judgment in question, are binary. All of mankind is separated into either believers ("sheep"), or disbelievers ("goats"). That is the only criteria that determines whether someone goes to Heaven or Hell.
To further establish that the "sheep" here are believers, we can look at John chapter 10, where Christ uses the term "sheep" extensively to refer to believers (John 10:1-29), culminating in the famous passage:
John 10:26-29
26 But ye believe not, because ye are not of my sheep, as I said unto you.
27 My sheep hear my voice, and I know them, and they follow me:
28 And I give unto them eternal life; and they shall never perish, neither shall any man pluck them out of my hand.
29 My Father, which gave them me, is greater than all; and no man is able to pluck them out of my Father's hand.
Above, the reason that the Jews Christ was addressing (John 10:19-24) aren't of His "sheep", is because they "believe not". Therefore, if a person believes, they are of His "sheep".
And finally, we see in the passage that those who go to "life eternal" are called "the righteous" (Matthew 25:37, 25:46).
Elsewhere in Scripture - and a topic this article is devoted to - we see that the only way to achieve the designation of "righteous" before God is to put one's faith in Jesus Christ, and be granted His imputed righteousness, because apart from that, "there is none righteous, no, not one" (Romans 3:10):
Romans 4:5
5 But to him that worketh not, but believeth on him that justifieth the ungodly, his faith is counted for righteousness.
Therefore, what we are seeing at this judgment in Matthew 25 is all of mankind being separated into believers and unbelievers.
The "sheep" are specifically those who were converted after the Rapture by the preaching of the Two Witnesses (Revelation 11:3), and the 144,000 other prophets (Revelation 7:3-4, 14:1-6) that God sends down during His wrath, though it reasonably may include those taken in the Rapture as well, and the saints who died prior.
At the judgment, the Lord praises the sheep, and then condemns the goats, based on their works, after declaring their respective fates:
Matthew 25:34-45
34 Then shall the King say unto them on his right hand, Come, ye blessed of my Father, inherit the kingdom prepared for you from the foundation of the world:
35 For I was an hungred, and ye gave me meat: I was thirsty, and ye gave me drink: I was a stranger, and ye took me in:
36 Naked, and ye clothed me: I was sick, and ye visited me: I was in prison, and ye came unto me.
37 Then shall the righteous answer him, saying, Lord, when saw we thee an hungred, and fed thee? or thirsty, and gave thee drink?
38 When saw we thee a stranger, and took thee in? or naked, and clothed thee?
39 Or when saw we thee sick, or in prison, and came unto thee?
40 And the King shall answer and say unto them, Verily I say unto you, Inasmuch as ye have done it unto one of the least of these my brethren, ye have done it unto me.
41 Then shall he say also unto them on the left hand, Depart from me, ye cursed, into everlasting fire, prepared for the devil and his angels:
42 For I was an hungred, and ye gave me no meat: I was thirsty, and ye gave me no drink:
43 I was a stranger, and ye took me not in: naked, and ye clothed me not: sick, and in prison, and ye visited me not.
44 Then shall they also answer him, saying, Lord, when saw we thee an hungred, or athirst, or a stranger, or naked, or sick, or in prison, and did not minister unto thee?
45 Then shall he answer them, saying, Verily I say unto you, Inasmuch as ye did it not to one of the least of these, ye did it not to me.
The works mentioned are:
This is the main point of contention in the passage. Those who teach works Salvation use these as a criteria for whether or not the sheep or the goats were designated as being one or the other in the first place.
The problem with that interpretation, besides ignoring all of the points made in the above section on the identity of the "sheep", is that if it were true, then we would be saved solely by keeping the Law, as all of the above works are encapsulated in the commandment to "love thy neighbor as thyself" (Matthew 22:39, Mark 12:31), which is a direct quotation from the Old Testament Law (Leviticus 19:18).
However, as is covered in detail in this article, Salvation by faith to the exclusion of the Law is one of the clearest points of doctrine in the entire New Testament, being testified to over and over again, almost to the point of redundancy (Acts 13:39, Romans 3:21-22, 3:28, 9:31-32, 10:4, Galatians 2:16, 3:11, 3:21-24, Philippians 3:9).
Rather, Christ mentions these works not because the lost never do them, or that the righteous always do them (or do them to some unknown degree that earns their way to Heaven), but because at this judgment, the Lord is only highlighting the good of the righteous, and the bad of the unsaved.
Surely, some of the goats did some, or even all of these things, at some point, but since "without faith it is impossible to please God" (Hebrews 11:6), that is not going to be mentioned here. Christ's mention of these works bears witness to the fact that if you are saved, God is interested in praising, honoring, and rewarding you, and if you aren't, all there is to look forward to is condemnation.
In summary, this passage does not teach that we get to Heaven based on whether or not we behave. Rather, it speaks to the binary outcome that all of mankind will face, the Lord's willingness to exalt believers, and the fact that Christ will rule over a literal kingdom on earth during the Millennium.