Question: If Jesus is the only way to heaven, then what will happen to the people in foreign countries who haven't heard of Him? How will God judge them?
Answer: God judges us according to our knowledge. Jesus acknowledged there would be many who wouldn't hear of Him when He said, "And this gospel of the kingdom shall be preached in the whole world for a witness to all the nations, and then the end shall come." (Matt. 24:14)
What about those who haven't heard? Romans 1:19-20 says, "That which is known about God is evident within them; for God made it evident to them. For since the creation of the world His invisible attributes, His eternal power and divine nature, have been clearly seen, being understood through what has been made, so that they are without excuse." This means that everyone has some knowledge of God, which is revealed through creation. If you look at a skyscraper you know that an architect designed it and someone built it, even though you weren't there to see it constructed. The complexity of the world reveals a Divine intelligence and Creator behind it.
Romans 2:14-15 gives us some more insight. God places a conscience within everyone to know right from wrong. "For when the Gentiles (pagans) who do not have the Law do instinctively the things of the Law, these, not having the Law, are a law to themselves in that they show the work of the Law written in their hearts, their conscience bearing witness, and their thoughts alternately accusing or else defending them. This means that God will judge those who haven't heard according to how they followed their consciences.
Jesus died for "the sins of the whole world" (1 John 2:2). He then commissioned His disciples to preach this good news to the entire world. Until that goal is accomplished, God will deal with those who haven't heard about Jesus according to their hearts. "In every nation the man who fears Him and does what is right is welcome to Him" (Acts 10:35). God will judge those who haven't heard according to how they followed the revelation that was given to them.
The real question is what about those who have heard about Jesus? You must either accept or reject Him. To find out how to know God in a personal way please read God's Plan of Salvation.