In this paper we generalize the concept of random networks to describe networks with non trivial features by a statistical mechanics approach. This framework is able to describe ensembles of undirected, directed as well as weighted networks. These networks might have not trivial community structure or, in the case of networks embedded in a given space, non trivial distance dependence of the link probability. These ensembles are characterized by their entropy which evaluate the cardinality of networks in the ensemble. The general framework we present in this paper is able to describe microcanonical ensemble of networks as well as canonical or hidden variables network ensemble with significant implication for the formulation of network constructing algorithms. Moreover in the paper we define and and characterize in particular the structural entropy, i.e. the entropy of the ensembles of undirected uncorrelated simple networks with given degree sequence. We discuss the apparent paradox that scale-free degree distribution are characterized by having small structural entropy but are so widely encountered in natural, social and technological complex systems. We give the proof that while scale-free networks ensembles have small structural entropy, they also correspond to the most likely degree distribution with the corresponding value of the structural entropy.