In nearly every train station there are places where you can buy snacks or portable meals to bring on board, but if you don't get something ahead of time there are also some trains on which you can get food during the trip.
Some trains have restaurant or cafe cars, and others have the train equivalent of flight attendants who roll through the aisles with beverages and snacks for sale. Options include pre-made sandwiches, bags of chips, yogurt, and various cookies and candies. Beverages available include coffee, soft drinks, water, wine, and beer. You can browse a fuller menu here.
Separate restaurant cars are more common on longer train trips—particularly long-haul international trains—and on newer, high-speed trains. In many cases, these cafe cars don't have tables or seats, but rather have counter areas at which passengers can stand while they drink coffee or eat a sandwich. These cars are designed more for quick bites, and you can also bring what you buy back to your seat.
On trains that have proper dining cars with tables and seats, service may only be provided at certain times. Seating in these cars is extremely limited. Less formal dining cars can usually serve light meals at any time.
When trains have trolley service, the food options are typically more like snacks than meals—things like bags of chips or cookies or pre-packaged sandwiches. In first class cars of some trains, passengers will get a free “welcome drink” and a snack upon boarding. Some train systems offer more choices for in-seat dining that can be ordered from the restaurant car, including hot dishes.
In all cases, food available at train stations and on board trains is much more expensive than what you could get at a cafe. The best option by far for budget travelers is to visit a supermarket before arriving at the train station to stock up on some train picnic provisions. Your options are more plentiful, often more delicious, and much cheaper—and eating at your seat is perfectly acceptable.
Note that options for people with dietary restrictions are extremely limited, especially on board the train. It's not possible to order meals as you can do on an airplane, so if you have a special diet your best bet is to purchase food ahead of time and bring it on the train with you.