A parallel passage for this Psalm is,
Jeremiah 17:5 Thus saith the LORD; Cursed be the man that trusteth in man, and maketh flesh his arm, and whose heart departeth from the LORD. 6 For he shall be like the heath in the desert, and shall not see when good cometh; but shall inhabit the parched places in the wilderness, in a salt land and not inhabited. 7 Blessed is the man that trusteth in the LORD, and whose hope the LORD is. 8 For he shall be as a tree planted by the waters, and that spreadeth out her roots by the river, and shall not see when heat cometh, but her leaf shall be green; and shall not be careful in the year of drought, neither shall cease from yielding fruit. 9 The heart is deceitful above all things, and desperately wicked: who can know it? 10 I the LORD search the heart, I try the reins, even to give every man according to his ways, and according to the fruit of his doings. 11 As the partridge sitteth on eggs, and hatcheth them not; so he that getteth riches, and not by right, shall leave them in the midst of his days, and at his end shall be a fool.The New Testament teaching is found in,
Hebrews 4:11-16 Let us labour therefore to enter into that rest, lest any man fall after the same example of unbelief. 12 For the word of God is quick, and powerful, and sharper than any twoedged sword, piercing even to the dividing asunder of soul and spirit, and of the joints and marrow, and is a discerner of the thoughts and intents of the heart. 13 Neither is there any creature that is not manifest in his sight: but all things are naked and opened unto the eyes of him with whom we have to do. 14 Seeing then that we have a great high priest, that is passed into the heavens, Jesus the Son of God, let us hold fast our profession. 15 For we have not an high priest which cannot be touched with the feeling of our infirmities; but was in all points tempted like as we are, yet without sin. 16 Let us therefore come boldly unto the throne of grace, that we may obtain mercy, and find grace to help in time of need.The key word in Psalms 139 seems to be thoughts (purpose, aim), both man's thoughts, v. 2, and God's thoughts, v. 17. These two verses are the only two times the word thoughts  is only used in Scripture.
The Psalm is easily divided into three distinct sections: vv. 1-6, 7-16 and 17-24.
O LORD, thou hast searched me, and known me. 2 Thou knowest my downsitting and mine uprising, thou understandest my thought afar off. (Ps. 139:1, 2.)This song of David proclaims that the Lord knows everything about us, even more than we know about ourselves. David is not proclaiming this out of fear of God, but out of awe --- that is, out of reverence and godly fear, v. 6.
Notice v. 5 indicates that David knew something was wrong; he confesses that no matter where one might go to avoid the Lord's hand, either his chastisements or his blessings, the Lord will find him. No one can go anywhere to avoid the lessons the Lord has for him or her.
17 How precious also are thy thoughts unto me, O God! how great is the sum of them! 18 If I should count them, they are more in number than the sand: when I awake, I am still with thee. 19 Surely thou wilt slay the wicked, O God: depart from me therefore, ye bloody men. 20 For they speak against thee wickedly, and thine enemies take thy name in vain. 21 Do not I hate them, O LORD, that hate thee? and am not I grieved with those that rise up against thee? 22 I hate them with perfect hatred: I count them mine enemies. 23 Search me, O God, and know my heart: try me, and know my thoughts: 24 And see if there be any wicked way in me, and lead me in the way everlasting. (Ps 139:17-24.)Here are some things to note from this last section:
First, v. 18, David proclaims his love for the word of God, meditating on it day and night.
Second, v. 19, David is confident that the Lord will catch up with the wicked. Not wanting to be caught in the Lord's move against wickedness, he wants to separate from wicked people and from his own wickedness.
Third, David loves and hates what God loves and hates.
Fourth, v. 20, there is an abundance of enemies who conspire against the Lord (his word). Their best efforts against the Lord are vain.
Fifth, v. 21, David is grieved with those who rise up against God.
Sixth, v. 22, David hates God's enemies with a perfect hatred, counting God's enemies as his enemies.
Seventh, David, the man who found the thoughts of God precious, realizes that there is sin in his own heart that needs to be dealt with. Thus we see that no matter how much we love God and his word, we are still in serious danger of our hearts departing from the Lord. Every indication here is that we depart without our knowledge.
David assumes that wrong thoughts and intents will creep into the heart and thoughts. Sin is man's natural state and enemy, so God must continually search the heart and thoughts of those who find God's thoughts precious to himself. It is evident from this Psalm that David was a man after God's own heart.But David knew that sin would creep in, so he prayed, Search me, O God, and know my heart: try me, and know my thoughts: And see if there be any wicked way in me, and lead me in the way everlasting.
Treasury of David gives an abundance of good quotes on this verse. However, I will only give a brief overview.
Search... Knowing the deceitful heart of man, David asks the Lord to search his heart:
He challenges the fullest investigation, the innermost search: he had need be a true man who can put himself deliberately into such a crucible. Yet we may each one desire such searching; for it would be a terrible calamity to us for sin to remain in our hearts unknown and undiscovered.(CHS, Online Bible.)
Try me... If one will be a servant of the Lord Jesus Christ, he must be willing to be honest, and let the Lord bring about the things needed to burn out, prune, the things displeasing to the Lord:
True faith is precious; it is like gold, it will endure a trial. Presumption is but a counterfeit, and cannot abide to be tried: #1Pe 1:7. A true believer fears no trial. He is willing to be tried by God. He is willing to have his faith tried by others, he shuns not the touchstone. He is much in testing himself. He would not take anything upon trust, especially that which is of such moment. He is willing to hear the worst as well as the best. That preaching pleases him best which is most searching and distinguishing: #Heb 4:12. He is loath to be deluded with vain hopes. He would not be flattered into a false conceit of his spiritual state. When trials are offered, he complies with the apostle's advice, #2Co 13:5. --David Clarkson.1) Only "a true man" will deliberately place himself in the refiner's furnace. Such action places one in the very hottest part of the battle, for the battle is submitting one's self to the Lord. However, if we want to be effective for the Lord, we must jump into the fire:
1 Peter 1:6 Wherein ye greatly rejoice, though now for a season, if need be, ye are in heaviness through manifold temptations: 7 That the trial of your faith, being much more precious than of gold that perisheth, though it be tried with fire, might be found unto praise and honour and glory at the appearing of Jesus Christ:
We must willingly submit ourselves to the flame; we must be willing to have the dross burned away if we want to bring praise and honour and glory to Jesus Christ.
James (1:13-18) makes it very clear that sin comes from within the sinner. It cannot be blamed upon the devil nor upon any outside source. The individual must take personal responsibility for sin.
James 4:1 From whence come wars and fightings among you? come they not hence, even of your lusts that war in your members? 2 Ye lust, and have not: ye kill, and desire to have, and cannot obtain: ye fight and war, yet ye have not, because ye ask not. 3 Ye ask, and receive not, because ye ask amiss, that ye may consume it upon your lusts. 4 Ye adulterers and adulteresses, know ye not that the friendship of the world is enmity with God? whosoever therefore will be a friend of the world is the enemy of God. 5 Do ye think that the scripture saith in vain, The spirit that dwelleth in us lusteth to envy? 6 But he giveth more grace. Wherefore he saith, God resisteth the proud, but giveth grace unto the humble. 7 Submit yourselves therefore to God. Resist the devil, and he will flee from you. 8 Draw nigh to God, and he will draw nigh to you. Cleanse your hands, ye sinners; and purify your hearts, ye double minded. 9 Be afflicted, and mourn, and weep: let your laughter be turned to mourning, and your joy to heaviness. 10 Humble yourselves in the sight of the Lord, and he shall lift you up.The problem is hidden sin --- areas not submitted to the Lord. If we want to bring praise and honour and glory to Jesus Christ, the very first thing that must be done is to humble ourselves under the mighty hand of God (1 Pet. 5:6.) -- that is, willingly cast ourselves into the refiner's fire, and let his divine providence work in our lives regardless of the cost..
2) The true believer "is willing to hear the worst as well as the best. That preaching pleases him best which is most searching and distinguishing." Thus one mark of a true Child of God is a willingness to listen to things that search him to the very heart and soul of the matter.
3) Many pray Psalms 139:23, 24, with "callous lips" --- that is, with no thought of it actually being worked in their lives. For this prayer from the heart will give a "sense of scalding." Though the prayer is true from Scripture, it seldom true from the heart, for if it is from the heart, the results will be seen and felt in the life of the petitioner; everyone has hidden sins that need the Lord's fire to burn away.
Christ has declared the difficulty, and solved it: "I am the way, the truth, and the life: no man cometh unto the Father, but by me." When the Son has made the sinner free, he is free indeed. The dear child, pardoned and reconciled, loves and longs for the Father's presence. What! is there neither spot nor wrinkle now upon the man, that he dares to challenge inspection by the Omniscient, and to offer his heart as Jehovah's dwelling place? He is not yet so pure; and well he knows it. The groan is bursting yet from his broken heart: "O wretched man that I am! Who shall deliver me from the body of this death?" Many stains defile him yet; but he loathes them now, and longs to be free. The difference between an unconverted and a converted man is not that the one has sins, and the other has none; but that the one takes part with his cherished sins against a dreaded God, and the other takes part with a reconciled God against his hated sins. He is out with his former friends, and in with his former adversary. Conversion is a turning, and it is one turning only; but it produces simultaneously and necessarily two distinct effects. Whereas his face was formerly turned away from God, and toward his own sins; it is now turned away from his own sins, and toward God. This one turning, with its twofold result, is in Christ the Mediator, and through the work of the Spirit.
As long as God is my enemy, I am his. I have no more power to change that condition than the polished surface has to refrain from reflecting the sunshine that falls upon it. It is God's love, from the face of Jesus shining into my dark heart, that makes my heart open to him, and delight to be his dwelling place. The eyes of the just Avenger I cannot endure to be in this place of sin; but the eye of the compassionate Physician I shall gladly admit into this place of disease; for he comes from heaven to earth that he may heal such sin sick souls as mine. When a disciple desires to be searched by the living God, he does not thereby intimate that there are no sins in him to be discovered: he intimates rather that his foes are so many and so lively, that nothing can subdue them except the presence and power of God. --William Arnot (--1875), in "Laws from Heaven for Life on Earth."4) The "scalding" from this prayer will result in being totally crushed in the spirit: O wretched man that I am! Who shall deliver me from the body of this death? (Rom. 7:24.) It is the broken who finally forsake the arm of the flesh, and who look to the Lord alone for his help.
5) The difference between the converted and unconverted is: one takes the side of the cherished sins against God; the other takes the side of God against the cherished sins. The converted supports God against his sin, and the unconverted supports his sin against God.
6) When a Child of God desires to be searched by the living God, he confesses that his foes are so many and so powerful that nothing but the presence and power of God can subdue them.
7) It takes great courage for a man to enter into his own heart, to look inside and combat the sin hidden and found therein. David desired to know his sin so he could be delivered from them.
8) David prayed to God to do this work in him, for God alone knows what is in the heart.
9) David desires to be tried by a competent judge: Who can understand his errors? cleanse thou me from secret faults. (Psalms 19:12.)
Do we want to bring praise and honour and glory to Jesus Christ?
We must willingly submit to his divine providence and chastisement, and cast ourselves upon his mercy.
And I will restore to you the years that the locust hath eaten, the
cankerworm, and the caterpiller, and the palmerworm, my great army which
I sent among you. (Joel 2:25.)