Q: Is fasting a command? Since the Bible says "when you fast..." not "if"...
A: No, fasting is not a command in Scripture. However, as the question points out, fasting was an expectation for disciples of Christ.
Fasting from food and other things is not an exclusively Christian activity. Several other religions practice some form of fasting for spiritual purposes while some doctors also recognize the purely physical benefits of fasting. Many today (particularly in an affluent nation such as the United States) scoff at the idea of not-eating as being a good option--much less an option that could be good for you!
Yet, when Jesus was teaching His disciples (and the crowd that had gathered) about the Kingdom of God, He assumed that fasting would be a part of their lifestyle.
In the sermon on the mount, recorded in Matthew 5-7, Jesus describes the nature of the kingdom of heaven and describes how "citizens" of the kingdom should live on the earth. In chapter 6, Jesus begins describing activities and values that Christians definitely value. He starts with giving to the poor:
So when you give to the poor... (Mat 6:2, NASB). Jesus assumes that subjects of the kingdom will give to the poor.
He continues, When you pray... (6:5). Jesus assumes that subjects of the kingdom will pray.
He then teaches the disciples to pray, warning them not to pray as the Gentiles do with meaningless repetition, but gives them a model to pray in accordance with the will of our Father (6:7-13).
Then Jesus continues by saying, Whenever you fast... (6:16). Jesus assumes that subjects of the kingdom will fast.
Although in some circles fasting is neglected or even frowned upon, Jesus considered this important spiritual discipline as worthy of being included in His sermon on what a kingdom lifestyle should include--along with other essentials such as giving to the poor and praying to our Father!
Since fasting was a normal activity for those to whom He was already speaking, He didn't give them a bunch of guidelines, but He did direct them to not fast like the hypocrites but to instead make sure that they weren't drawing attention to themselves through this discipline. Fasting is not an activity to be done to gain pity from others or to make a show of piety! Fasting, like praying and giving, is to be done for the Lord and in order to advance His kingdom and grow in our relationship and dependence upon Him.
Q: Are there guidelines to fasting as for what duration, how frequently, from what, etc.?
A: Yes and no. (Thanks for clarifying!)
There is no specific passage in the Bible that outlines exactly how to fast on all occasions (that I am aware of). Most of the examples we see in the biblical accounts are individuals or groups who are fasting for various reasons.
For example, we see the sons of Israel fasting out of sorrow (Judges 20:26); fasting due to repentance (1 Sam. 7:6); fasting as an act of mourning (1 Sam. 31:13); fasting in intense times of seeking the Lord (2 Sam. 12:16); and fasting as an act of humility before the Lord to seek His favor (Ezra 8:21), among other examples.
We also see how not to fast. Jesus taught against fasting like the hypocrites, so that everyone knows you are "suffering for the Lord" (Matt. 6:16-18). The Lord speaks through the prophet Isaiah and decries fasting as a merely "religious" activity -- something done merely as an act of obligation and in a ritualistic fashion with no regard to seeking the Lord's will (Isaiah 58).
Through these texts a picture begins to emerge that fasting is meant to humble us before the Lord and to demonstrate our dependence upon Him for sustaining our very existence (Deuteronomy 8:3; John 4:31-34). Fasting, therefore, should never be done as an attempt to manipulate the God of the universe into doing our will, but instead is an act of humbling ourselves before the Lord in order to seek His will and rely on Him to accomplish His purposes through us for His glory.
In short, fasting should be done as a response to our relationship with the Lord and always at His leading. While fasting from food is the most normal biblical practice, it is possible (and not anti-biblical) that we would abstain (fast) from anything that the Lord leads us to abstain from for a time set by Him.
One final comment: fasting is reserved for activities and things that are not inherently sinful. Eating is a necessity to live, but can be abstained from for a time without serious physical repercussions (although there are certainly some health conditions which should be taken into account before engaging in a fast from food). Another example: while it may be permissible to watch television for a Christian, it may be beneficial to fast from such activities for a time to devote yourself to prayer. While fasting is a separate discipline from prayer, the two are often combined together in the Scriptures and there is certainly value in combining these practices in our walk with the living God.
*For further reading on this topic, see the Didache, chapter 8, available online here, and Richard Foster's chapter in Celebration of Discipline on Fasting (pp. 47-61; available in the church library).