Internet in the trains
Many long distance trains are equipped with 3G/4G signal amplifiers. If you have a phone with internet access, you can use this improved signal. There is no Wifi.
You can use your smartphone to pick up the 3G/4G signal and configure the phone as a Wifi hotspot. That allows Wifi enabled equipment such as a laptop or a tablet to access the internet. Buy an appropriate SIM card to save money.
Free internet at the stations
About 80 rail stations offer Wifi that can be accessed for an hour at no cost. You can connect to the Wifi network and start your browser. You can request an access code that will be delivered through a text message. This works on international phones as well, except if your provider prohibits receiving text messages in Switzerland. Two hours after the hour has ended one can use it for another hour.
Examples of stations offering this service: Basel SBB (main station), Bern, Geneva, Lucerne, Montreux and Zurich HB (main station).
Internet in other places
Nearly all hotels offer free or paid Wifi. Mobile internet is available in nearly all inhabited areas. Don't expect internet to be available in remote areas. If internet connectivity is important to you, be sure to arrange for a Swiss SIM card or mobile hotspot.