The correct way to write the sentence is option B: "The class went to visit a farm, and they saw cows being milked."
This option correctly uses a coordinating conjunction ("and") to connect two independent clauses, ensuring proper punctuation with the comma before the conjunction. Option D is also correct, as it is the same as B, but duplicated here indicates it could be a valid answer.