Covariance is calculated by summing up the products of the differences between corresponding values of two variables, and then dividing by the total number of data points minus one.