We came to Ascona by public bus no. 1 from Locarno railway station. The bus takes close to 20 minutes to reach Ascona post office which is the end of the line. Buy your ticket from the vending machine at the bus stop or from the driver. If you travel on some sort of Swiss pass ask whether it's included.
The bus line no. 1 starts in Tenero, then travels via Locarno to Ascona. Another useful bus stop in Locarno is the one to the right side of the tourist office by the Casino Locarno.
Unfortunately, it's not so pleasant to walk to Ascona from Locarno as there is no bridge right by the river delta and you need to go inland to use the next possible bridge. Otherwise it would make a nice walk by the lake. I did walk to Ascona some years ago but I cannot really recommend it.
Another option is to take a ship.
I actually wondered whether to put this tip under transportation or rather under things to do. I guess it is something that one should do in nice weather and when one has enough time.
I once took a ship to the Brissago islands from Ascona which makes a real nice outing. There are various types of ships depending on your destination. If you intend to travel to Arona at the other end of the lake or Stresa, then you are likely to use a fast boat like the one on my photo.