Railway companies can interact with and control others in many ways. These relationships can be complicated by bankruptcies.
Often, when a railroad first opens, it is only a short spur of a main line. The owner of the spur line may contract with the owner of the main line for operation of the contractee's trains, either as a separate line or as a branch with through service. This agreement may continue as the former railroad expands, or may be temporary until the line is completed.
If the operating company goes bankrupt, the contract ends and the operated company must operate itself.
A major railroad may lease a connecting line from another company, usually the latter company's full system. A typical lease results in the former railroad (the lessee) paying the latter company (the lessor) a certain yearly rate, based on maintenance, profit, or overhead, in order to have full control of the lessor's lines, including operation.
If the lessee goes bankrupt, the lessor is released from the lease.
Most railroad companies are publicly traded with stocks. As the control the company, one railroad company can buy a majority of of another one in order to control it. Sometimes a bridge line, a railroad that has a majority of traffic coming from points not on its line, is owned equally by the companies that use it (via trackage rights).
Stock ownership does not automatically result in merge of operations, merely in friendly policies towards each other. Operating and leasing agreements typically require a more stringent approval process through the regulating body.
If the owned company goes bankrupt, its stock is worthless, and the owner no longer controls it (unless it buys it back at auction).
Consolidation happens when two railroad companies are consolidated. It is often the last step in an arrangement between two railroads, and is hard to undo, except in the case of bankruptcy, when different parts of the railroad may be sold to different buyers at auction.
Trackage rights (US), running rights or running powers (UK) is an agreement between railroad companies in which the owner of tracks grants another railroad company some use of them. These deals can be long- or short-term; can include the right to serve customers on the line or not; and can be exclusive or not.