The primary function of hemoglobin is to transport oxygen from the lungs to various tissues in the body and to carry carbon dioxide from the tissues back to the lungs.