This is a form of N-tier architecture . Initially, most sites will begin as a 2-tier architecture, with a web server and database. The web server will serve the pages that the user sees, while these dynamic pages access the database.
When the architecture is divided into several levels, usually you will have a web application in front, with some kind of application server that will invoke the web application. This application server contains business logic and makes the application functional. The “face” for the application simply makes things look beautiful and formats it for display to users.
An application server or “middleware” is simply a collection of functions that can be called by web servers. Middleware should be powerful for a site such as twitter, where most of the activity when sending messages, and from people accessing the website, is not so much.
The interconnection between the tiers is probably the standard web service technology, but it could be the usual thing, perhaps REST or some other kind of web service where the web application (Ruby on Rails) can access the application data.
In this type of architecture, Rails certainly categorized all ORM and data management as middleware.
Middleware is also an indefinite term and can consist of many levels, which is why they are called N-Tier. I would argue that Twitter has another part of its messaging middleware, while the other part is about managing accounts, receiving messages, etc.
The idea is to be able to scale up by adding hardware, where you can have a cluster of servers serving web pages, another messaging with the cluster, and then a cluster of database servers that supports all of this. This is not an exact science, each architecture is different, but, as a rule, you can think about it.