The Correlation Function in Redshift Space: General Formula with Wide-Angle Effects and Cosmological Distortions