To obtain the Surf HM, you must first defeat Jasmine, the Olivine City Gym Leader. Jasmine specializes in Steel-type Pokémon, so bring Pokémon with Fighting, Ground, or Fire-type moves.
After defeating Jasmine, go back to Professor Elm's Lab in New Bark Town (located on the north side of town).
Professor Elm will give you the Surf HM, which can teach the move Surf to a compatible Pokémon.
Open your Bag, select the HM section, choose Surf, and select the Pokémon you want to learn it. Note: Some Pokémon cannot learn Surf due to their typing (e.g., Octillery, Quagsire, Azumarill, Starmie).
Once taught, select Surf from your Pokémon's move list while standing next to water to travel across it.