James has just told us to rejoice when we fall into trials, realizing that tribulation develops perseverance, which in turn produces maturity. In what seems like an abrupt intrusion, he tells us that if we lack wisdom, we should ask God for it. The connection, however, is clear: If ever we need wisdom, it is when we are going through rough times and are tempted to sin.
The beginning of wisdom is the fear of God (Proverbs 1:7). For the Greeks, philosophy (love of wisdom) meant speculation and doubt. According to the Bible, such a posture is folly. Wisdom and true philosophy start with firm knowledge—the knowledge of God. Moreover, wisdom is not obtained from meditation and contemplation so much as from prayer: "He should ask God," says James. As we are actively engaged with God we obtain wisdom.
Taken out of context, James' statement is dangerous. Does James mean that all we have to do is ask God for instant wisdom and we shall receive it? Not at all. James assumes we know the book of Proverbs, which repeatedly and constantly exhorts us to study to know wisdom, to seek it diligently, to take every opportunity to learn it, and to desire it above all else. James is saying that God will give wisdom to those who prayerfully and diligently seek it.
Wisdom comes from two things. The first is the careful study of God's Word, pursuing the twin concepts of doctrine and law. Wisdom grows from knowledge when, second, we get experience putting God's ways into practice. Often this involves tribulation, and so wisdom grows from our experiences with trials and difficulties, provided that in the midst of our trials we lean on God's Word.
James goes on in verse 6 to say that prayer for wisdom must be made in faith and not in doubt. There is a heretical interpretation of this verse abroad today which says that the "prayer of faith" means we ask God for something and then assume He has given it and we should act accordingly. We ask God for a million dollars and then act in faith by charging up our credit cards. This is magic, not faith. James means that we are to ask God with a trusting attitude, regardless of whether His answer to our prayer is yes or no.
As has been taught, true wisdom is the combination of knowledge and practical experience. It requires involvement of people in the service of Christ's disposal, allowing Him to deploy you in a variety of experiences where both wisdom and godly maturity may result.