Why did God create the Heavens and the Earth? Genesis 1:1 


  In the beginning God created the heavens and the earth.


Why ?

Does the Bible state a reason anywhere?
God created the heavens and the earth as an expression of His glory.


  The heavens are telling of the glory of God; 
  And their expanse is declaring the work of His hands. (Psalms 19:1 NASB)


and


  The heavens declare His righteousness, 
  And all the peoples have seen His glory. (Psalms 97:6 NASB)


I think John Piper explains it nicely in Desiring God, which is available for free online.  Basically, he argues that God's ultimate purpose and joy is in the display of His glory:


  God would be unrighteous (just as we would) if He valued
  anything more than what is supremely valuable. But He Himself is supremely valuable. If He did not take infinite delight in the worth of His own glory, He
  would be unrighteous. For it is right to take delight in a person in proportion to
  the excellence of that person’s glory. (Desiring God, p42-43)


And to that purpose, creation is the expression of His glory:


  In creation, God “went public” with the glory that reverberates joyfully between
  the Father and the Son. There is something about the fullness of God’s joy that
  inclines it to overflow. There is an expansive quality to His joy. It wants to share
  itself. The impulse to create the world was not from weakness, as though God
  were lacking in some perfection that creation could supply. “It is no argument of
  the emptiness or deficiency of a fountain, that it is inclined to overflow.”
  
  God loves to behold His glory reflected in His works. So the eternal happiness of the triune God spilled over in the work of creation and redemption. And
  since this original happiness was God’s delight in His own glory, therefore the
  happiness that He has in all His works of creation and redemption is nothing
  other than a delight in His own glory. This is why God has done all things, from
  creation to consummation, for the preservation and display of His glory. All His
  works are simply the spillover of His infinite exuberance for His own excellence. (Desiring God, p44)


Aside Note:

To some, this concept might seem to put God in a light in which he seems vain, or like he is "just showing off" (as @MasonWheeler mentioned). I don't believe it does. 

Although "showing off" is essentially correct, that phrase carries a negative connotation.  I think that connotation comes from the fact that "showing off" is usually used to describe humans that scramble to draw attention to, and highlight their own perceived glory.  The difference between these people and God is that:


As humans, these people do not actually deserve the attention
These people attempt to detract glory from God, who deserves it
Their intentions are entirely self serving, and seek attention that benefits only themselves


On the other hand:


God very much deserves our attention
God cannot detract glory from Himself
It is in our infinite best interest to perceive and experience God's glory.  That is why we are constantly commanded to praise God, give glory to God, etc.