We study random surfaces which arise as height functions of random perfect matchings (a.k.a. dimer configurations) on a weighted, bipartite, doubly periodic graph G embedded in the plane. We derive explicit formulas for the surface tension and local Gibbs measure probabilities of these models. The answers involve a certain plane algebraic curve, which is the spectral curve of the Kasteleyn operator of the graph. For example, the surface tension is the Legendre dual of the Ronkin function of the spectral curve. The amoeba of the spectral curve represents the phase diagram of the dimer model. Further, we prove that the spectral curve of a dimer model is always a real curve of special type, namely it is a Harnack curve. This implies many qualitative and quantitative statement about the behavior of the dimer model, such as existence of smooth phases, decay rate of correlations, growth rate of height function fluctuations, etc.