Deutsche Bahn AG (DB) is the German national railway company. Virtually all active train stations, all long-distance trains and and many regional trains and S-Bahn city trains are run by them.
Most regions of Germany offer some kind of transportation network, local DB trains are usually included and tickets from both of them are valid, although the pricing might be very different.
The normal fares are very expensive and usually not used by locals. The DB is actualy the 2nd most expensive railroad in Europe after the Norwegian. There is a wide range of special offers and rebate systems, but they are rather difficult to use for single spontaneous trips. Some exceptions are the Schönes-Wochenende-Ticket (Happy-Weekend-Ticket), the Quer-durchs-Land Ticket and the regional day ticket Länderticket.
The Schönes-Wochenende-Ticket, usually just called Wochenendticket which is valid for a whole Saturday or Sunday for up to 5 persons in all regional trains in whole Germany. They are 40 Euro. This means, you can actually get i.e. from Munich to Hamburg for 8 Euro, but it will take you about 12 hours. The Quer-durchs-Land Ticket does the same on weekdays but its price depends of the number of people which you have to decide forehand. You pay 42 Euro for the first person and 6 Euro more for each subsequent. On weekdays you can use the Länderticket, which is about 20 Euro for one person or 30 Euro for 5 persons in all regional trains from 9:00 unto 3:00 the next day in a certain Federal State (sometimes several count as one).
You can travel with other people's group tickets (like the Wochenendticket) as these tickets are valid for up to 5 people and the number of passengers is not specified on the ticket. Just ask people on the platform if you can join their ticket before they enter the train. You can also just get on the train and hope to find people there but bear in mind that if you don't find anybody until the ticket inspector sees you, you're a blackrider. Some inspectors have also made a fuss because the people going with the ticket should be clear from the beginning of the trip, but that's rare. Some people might want you to chip in some money. In that case you can try to find somebody else as most people take you for free and, if you ask them, even give you their ticket when they are at the end of their journey.
It is also possible to join Ländertickets, but they are not as common and there is a cheaper Single version which is not useful because you cannot join it.
In some regions like Dresden, Cologne and Aachen people are also allowed to take somebody on their student ticket, job ticket, monthly ticket or year ticket in afternoons and on weekends. This might be noted in the city articles.
On long distance trains (IC and ICE) you can buy a ticket from the conductor, which makes blackriding almost impossible. The fare will be 10% higher though. If you fall asleep (or pretend to), there is a chance that the conductor doesn't want to wake you up. Wearing headphones might help. Check the fares beforehand anyway, because they are pretty high.
In regional trains (S-Bahn, RegionalBahn or RegionalExpress) blackriding mostly doesn't make much sense, because chances that you reach your destination without getting into some kind of trouble are rather low. Try hitchhiking instead ;)
On some regional trains, there is a ticket machine. This varies from region to region, so you should check it before. If there is, you could try checking if there actually is a conductor on the train before buying a ticket. Multi-wagon trains and conductors joining the train at a later stop make this quite risky though. You're obligated to posses a valid ticket as soon as you join the train so you can get fined even in the process of buying if you're considered a blackrider.
- if they get your ID, it means you'll be donating from 40 EUR to the full ticket price to the DB's shareholders (mostly the German state, if that makes you feel better) − if they don't, you might be tempted to give them a fake name and address, which is probably illegal
- You don't have to pay instantly, but usually by bank transfer within 10 days
- As a last line of defense, conductors can call the police and legally keep you from leaving to prevent you from "illegally obtaining a service"
- It's unclear if there are special rules if you don't possess a German ID