Psalm 34:18 (ESV) … “The Lord is near to the brokenhearted and saves the crushed in spirit.”
The favor of God rests upon the righteous, the brokenhearted, the crushed in spirit. In every eventuality of life, the Lord is near. Here we note again that the psalmist is well acquainted with reality. Those who trust in God are not shielded from all troubles—A righteous man may have many troubles, but the Lord delivers him from them all.
Is this categorical statement to be taken as absolute? What of those who have been persecuted or martyred for their faith? What of the execution of Jesus of Nazareth? What deliverance was theirs? Our psalm indicates, that “God’s presence is experienced within these crisis situations; there is no divine guarantee that the righteous will escape the crises and trials of mortal existence. The divine presence … made possible triumph in the midst of trial.”
From the NT disclosure of resurrection, we are aware of the reality of deliverance beyond death. Whether or not this was in the mind of the psalmist we cannot tell, but in verse 20 he speaks of the physical protection that God provides for his people - “He guards every bone of his body” (NEB).
Throughout, the emphasis is upon the contrast between the fate of the wicked and that of the righteous. Destruction comes to the former, deliverance to the latter. Evil will slay the wicked. Those who do evil will be slain by evil, but the Lord redeems his servants; no one will be condemned who takes refuge in him. This was the experience of the psalmist. He believed that it could be that of others as well. 
In summary, David doesn’t suggest that the life of faith and obedience will exempt the child of God from trouble. However, David does promise that, if we trust the Lord and call upon the Lord, that the Lord can see us through our troubles and make them a blessing to us and through us to others.