Taxi, Business hotels, Manga kissa (Internet comic cafe) are some options where you can spend night
If you miss the train - which happens many times then there are couple of options..
1. If you are closeby your hotel or place of stay then you can take a cab. After 11, the cab will charge an extra 10-15% depending on if it is a company taxi or personal /private taxi (you will find this mark when it is personal /private taxi（個人） ) (these days many taxis are trying not to charge this extra amount to get more customers)
2. if taxi option is expensive, then there are foll. options
a. business hotels - there are business hotels which fall in the range of 4000 - 6000 JPY per night (so if the taxi is going to cost 12000 JPY or so then it makes sense in staying at a business hotel)
b. Manga kissa (Internet comic cafe) - these are cafes with reclining chairs - internet connection - magazines - manga - which serve free coffee - some cup noodles (paid) etc but for as low as 500 JPY or 1000 JPY or so you can spend a night here and sleep on the reclining chair. For an additional 300-500 JPY you can also take a shower here.
c. if you are close to a 24*7 McDonald or some place then you can spend time there as well
(during earthquake, some people purchased bicycles and went home. A bicycle would cost at 8000 JPY or so - as at that time taxi was unavailable and business hotels were closed or crowded - so this can be also an option. You can buy a bicyle from Don Quixote (Don-Ki-Hote) shops)
The first train starts at around 5:30 or so - depending on which train you take there might be a slight difference.