We present an analytical approach to calculate the hydrodynamics of the interaction between a relativistic ejecta and a surrounding medium, whose evolution serves as a model for Gamma-Ray Burst afterglows. We investigate the effect of the relevant model parameters on the X-ray and optical fluxes, and the effect of a delayed energy input and anisotropy in the ejecta on the shape of the light curves. We compare our numerical results to observed afterglows and give a quantitative description of the conditions (geometry and physical parameters) in the ejecta that are compatible with the X-ray and optical light curves of the 970508 afterglow, for which a large number of accurate flux measurements are available.