The thermodynamics of the diffuse, X-ray-emitting gas in clusters of galaxies is determined by gravitational processes associated with infalling gas, shock heating and adiabatic compression, and nongravitational processes such as heating by supernovae, stellar winds, activity in central galactic nuclei, and radiative cooling. The effect of gravitational processes on the thermodynamics of the intracluster medium (ICM) can be expressed in terms of the ICM entropy. The entropy is a convenient variable as long as cooling is negligible, since it remains constant during the phase of adiabatic compression during accretion into the potential well, and it shows a single steplike increase during shock heating. Observations indicate that nongravitational processes also play a key role in determining the distribution of entropy in the ICM. In particular, an entropy excess with respect to that produced by purely gravitational processes has been recently detected in the centers of low-temperature systems. This type of entropy excess is believed to be responsible for many other properties of local X-ray clusters, including the L-T relation and the flat density cores in clusters and groups. In this paper we assume that the entropy excess is present in the intergalactic medium (IGM) baryons before the gas is accreted by the dark matter halos and reaches high densities. We use a generalized spherical model to compute the X-ray properties of groups and clusters for a range of initial entropy levels in the IGM and for a range of mass scales, cosmic epochs, and background cosmologies. In particular, we follow the formation of adiabatic cores during the first stages of the gravitational collapse and the subsequent evolution of the central entropy due to radiative energy loss. The model predicts the statistical properties of the cluster population at a given epoch and also allows study of the evolution of single X-ray halos as a function of their age. We find that the statistical properties of the X-ray clusters strongly depend on the value of the initial background entropy. Assuming a constant, uniform value for the background entropy, the present-day X-ray data are well fitted for the following range of values of the adiabatic constant: K* ≡ kBT/μmpρ2/3 = (0.4 ± 0.1) × 1034 ergs cm2 g-5/3 for clusters with average temperatures kT > 2 keV and K* = (0.2 ± 0.1) × 1034 ergs cm2 g-5/3 for groups and clusters with average temperatures kBT < 2 keV. These values correspond to different excess energy per particle of kBT ≥ 0.1(K*/0.4 × 1034) keV. The dependence of K* on the mass scale can be well reproduced by an epoch-dependent external entropy: the relation K* = 0.8(1 + z)-1 × 1034 ergs cm2 g-5/3 fits the data over the whole temperature range. The model can be extended to include internal heating, but in this case the energy budget required to fit the X-ray properties would be much higher. Observations of both local and distant clusters can be used to trace the distribution and the evolution of the entropy in the cosmic baryons and to constrain the typical epoch and the source of the heating processes. The X-ray satellites Chandra and XMM can add to our knowledge of the history of the cosmic baryons, already derived from the high-redshift, low-density gas observed in the QSO absorption-line clouds, by imaging the hot, higher density plasma observed in groups and clusters of galaxies.