How to know whether God truly spoke to you? In many churches nowadays, there are a lot of people claiming to have the gifts of the holy spirit(prophets, speaking in tongues etc). In my experience, most of them are just looking for personal gain that's why I started thinking they are all false prophets.

However, recently I had an experience which I believe truly is a message from God through a prophet(see first edit for more details).

That's why I am asking what are some biblical ways to test whether someone is speaking from God(guided by the Holy spirit) or by themselves?
Summary

The verses below tell you clearly what to do. Use logic and reason, consult with Christians wiser than you, and test this man's words. Praise God if he passes. Rebuke the man if he does not.

Modified from a previous answer of mine on testing prophets

The first thing I want to say is that if your wife is a Christian with a moderately strong faith you should tell her immediately. She should be your guide through this. That will be the extent of the personal advice I will give.

The Bible says a great deal about testing prophets and spirits. From your story, the man, whom you did not know, came up to you uninvited and told you information about yourself that he could not have known. I assume he also had a message for you of some sort, else what would be the point. He has performed an act of prophesy.

A similar situation from the Bible is when Jesus speaks to the Samaritan woman at the well in John 4. Jesus, using the give of prophecy, was able to know things about the woman and give her a message that she then likely told to the whole town (that message was that the messiah was here).

Now the following is from the Bible on how to test spirits and prophets. Much of this can be applied to your situation.

A spirit will confess that Jesus Christ has come in the flesh.


  Dear friends, do not believe every spirit, but test the spirits to see whether they are from God, because many false prophets have gone out into the world. 2 This is how you can recognize the Spirit of God: Every spirit that acknowledges that Jesus Christ has come in the flesh is from God, 3 but every spirit that does not acknowledge Jesus is not from God. This is the spirit of the antichrist, which you have heard is coming and even now is already in the world.
  1 John 4


Although this verse says 'spirit' I think it is just as easily applied to prophets. Both are charged with speaking God's words.

This verse also not only says it is okay to test the spirits but implies that you should every time. You should be comfortable with the fact that this event has happened and even more comfortable that you are approaching it with a healthy amount of skepticism. Assuming you are a Christian, you are equipped to discern the divinity of this event, however, part of that equipping is consulting other Christians that you know. Proverbs 27:17

A person speaking by the spirit will claim Jesus is Lord


  3 Therefore I want you to know that no one who is speaking by the Spirit of God says, “Jesus be cursed,” and no one can say, “Jesus is Lord,” except by the Holy Spirit. 1 Cor. 12:3


This is pretty straight forward. Did this person use any language that implies this? In the best case scenario you can question this person directly: "Is Jesus Christ the Lord of all?" He should be able to easily confess Christ is Lord and praise and worship Him with you. Since you were at a church that is a good sign, however, it would be best if you saw him worshiping or otherwise confessing the sovereignty of Christ.

A prophet will not contradict God's word


  20 Consult God’s instruction and the testimony of warning. If anyone does not speak according to this word, they have no light of dawn. Isaiah 8:20


Praise God for giving us His word! Full of His wisdom! You have the Bible and can easily consult it concerning what this person told you. From your story, it seems like you are struggling with something, such as a difficult decision or perhaps how to reconcile a sin. The Bible has much to say on many topics. If there is anything that this person said that contradicts His Word, than I am afraid he was not speaking from the Spirit.

A prophet will not prove himself with wonders then ask you to worship other Gods


  If a prophet, or one who foretells by dreams, appears among you and announces to you a sign or wonder, 2 and if the sign or wonder spoken of takes place, and the prophet says, “Let us follow other gods” (gods you have not known) “and let us worship them,” 3 you must not listen to the words of that prophet or dreamer. Deut 13:1-3


I very much doubt that occurred in your story, but just in case.

The predictions of a prophet must come to pass


  20 But a prophet who presumes to speak in my name anything I have not commanded, or a prophet who speaks in the name of other gods, is to be put to death.”
  21 You may say to yourselves, “How can we know when a message has not been spoken by the Lord?” 22 If what a prophet proclaims in the name of the Lord does not take place or come true, that is a message the Lord has not spoken. That prophet has spoken presumptuously, so do not be alarmed. Deut 18:20-22


These verses imply that an already proven prophet can later be unproven. This backs up 1 John's claim to test them every time and not just the first time. Apparently saying God said something that He did not is a capital offense, so you could argue that a prophet of ancient times was very slow to speak.

Now, if this person said anything about what will be, that must come true. There is no exception. If his would be predictions do not come true he was not speaking from the Spirit.

A true prophet will live a righteous life


  15 “Watch out for false prophets. They come to you in sheep’s clothing, but inwardly they are ferocious wolves. 16 By their fruit you will recognize them. Do people pick grapes from thornbushes, or figs from thistles? 17 Likewise, every good tree bears good fruit, but a bad tree bears bad fruit. 18 A good tree cannot bear bad fruit, and a bad tree cannot bear good fruit. 19 Every tree that does not bear good fruit is cut down and thrown into the fire. 20 Thus, by their fruit you will recognize them. Matt 7:15-20


This will likely not help you much, but it is still worth noting. If you can learn something about his life and lifestyle then you can make a determination whether he was doing his best to live a Christian life.

Conclusion

This verses tell you clearly what to do. Use logic and reason, consult with Christians wiser than you, and test this man. Praise God if he passes. Rebuke the man if he does not.