This long thread has been summarized and more info added, in the Railroads! wiki page on Maintenance. You should see that, for Maintenance info (and add to it, if you like!). The end of this thread now serves for discussion about Maintenance.
Good railroading! - RK
*** END UPDATE ***
Basics
* There are 4.8 maintenance instances a year (25/5.2). Since this doesn't precisely divide into 12 months, you will see maintenances occurring on various months; it won't be a repeating pattern of months, except over long time scales.
* The "Last Year" (LY) Maintenance value (seen in F4) is actually the last 5.2 years worth of maintenance, a.k.a. the total for the last 25 maintenances. These 5.2-year periods are important to how maintenance works, so I'll call them the Maintenance Period (MP).
* Thus, the Lifetime (LT) maintenance cost (F4) is a better estimate of, well, lifetime costs - IF you haven't upgraded your engine. The LT cost shows you maintenance for the route, not the train. You have to start a new route if you want it to be just for that engine.
* As of exactly 5 years, you will have incurred 24 times the Maintenance Cost (MC). Then the next MP kicks in with the 25th maintenance (at 5.2 years). If you have the Lube Patent, $275 is added to your Maintenance cost through that period; if you don't, $550 is added. So a Grasshopper's MC (initially $550) is increased to $1,100 for the second MP, $1,650 for the third, etc.
* If you slow down the game and watch the Profit for a train more than 5 years old, you will see an odd thing. The Profit will appear to go up briefly (even though the train has not made a delivery) and then go back down. What's happening is the maintenance cost incurred 25 instances ago is being dropped, then the new MC being imposed. It's a "first in, first out" function. By noting the prices before (initial value), during, and after (final value) this event, you can see what I mean. The value that got dropped will always be $275 less than the current maintenance cost if you have the Lube Patent, or $550 if you don't. Across this little price jag, an MC that's $275 or $550 less than the current MC got dropped and one that's $275 or $550 more than that previous one got added (the current MC), so the net difference (final value-initial value) is always $1,100 (without Patent) or $550 (with Patent) across the little jag.
* If you have upgraded an old train (with high MCs), those previous, high MCs will be dropping out, until the train is 5 years old (at which point the "proper" ones for the current train make it to the end of that queue). So it can look weird if you don't know what's going on.
* Example: When a Grasshopper is a little over 10 years old, it's LY will say ~$27500 (25*1100), but it's LT will say ~$41,250 (25*550 + 25*1100). LY is showing the last 5.2 years, where the MC is $1,100, but LY covers both MPs.
* The Lube Patent not only reduces the initial MC fee, but also the increase imposed each MP. MC is increased by $550 (w/o Patent) or $275 (w/Patent) each new MP for all engines, regardless of the train's initial MC.
* Trade-In Values use a hyperbolic function. For the percent of original engine cost remaining, use: 1 - (25/(Mo#+25)), where Mo# is the number of months transpired. It falls quickly, then slows; you've lost 50% of value at the two-year point (actually 25 months), but very old trains will retain a few percent of their original cost.
Practical Usage
In order to best utilize this info, the basic question is, "when should I upgrade my trains?"
I re-did the finances another way which I'm sure is better. Train replacement times are a lot more reasonable now ...
As a straw man, the point where they should be upgraded is when the total maintenance cost (the LT value, if you haven't upgraded) equals the cost of a new engine, minus the trade-in value (TIV). This is then averaged across time by dividing by the number of years that have passed. The minimum (across a wide span of years) is the point where you spend the least money per year, given all three costs. It's a very smooth curve; generally any time within 10 years of the stated minimum is fine, except for the very earliest engines.
On to the good stuff...
Without the Lube Patent, optimum replacement times are 10.2 years (GH, Norris), 15.3 years (American to Consolidation), 20.6 years (Pacific to F-Series), and 25.8 years (GS and GP-Series). With the Lube Patent, the GH is 10.2, Norris 15.4 ... anyway, get the attached spreadsheet.

While the optimum times might look disquietingly similar (given differing engine and maintenance costs), what does differ for the trains above, is the average cost per year ((New engine - TIV + LT MC) / Years). As stated above, it's a smooth curve around the minimum, and with the later trains, it doesn't make a huge difference if you wait 10, 20, or even 30 years past the optimum. The GP-Series (plus Lube) has an optimum average yearly cost of $22,388 at 36.2 years, but at 72 years, the average yearly cost has only risen to $24,450.
Attached, find an Excel spreadsheet with engine stats, including recommended replacement time. I will post an equation for figuring LT cost at any point in time, when the wiki is up.