There are a few ways in which large caves can form.
The first one is lava tubes
A lava tube is a natural conduit formed by flowing lava which moves beneath the hardened surface of a lava flow. Tubes can drain lava from a volcano during an eruption, or can be extinct, meaning the lava flow has ceased, and the rock has cooled and left a long cave.
A lava tube is a type of lava cave formed when a low-viscosity lava flow develops a continuous and hard crust, which thickens and forms a roof above the still-flowing lava stream. Tubes form in one of two ways: either by the crusting over of lava channels, or from pāhoehoe flows where the lava is moving under the surface.
Lava usually leaves the point of eruption in channels. These channels tend to stay very hot as their surroundings cool. This means they slowly develop walls around them as the surrounding lava cools and/or as the channel melts its way deeper. These channels can get deep enough to crust over, forming an insulating tube that keeps the lava molten and serves as a conduit for the flowing lava. These types of lava tubes tend to be closer to the lava eruption point.
The other is through karst
Karst is a topography formed from the dissolution of soluble rocks such as limestone, dolomite, and gypsum. It is characterized by underground drainage systems with sinkholes and caves. It has also been documented for more weathering-resistant rocks, such as quartzite, given the right conditions.
The development of karst occurs whenever acidic water starts to break down the surface of bedrock near its cracks, or bedding planes. As the bedrock (typically limestone or dolomite) continues to degrade, its cracks tend to get bigger. As time goes on, these fractures will become wider, and eventually a drainage system of some sort may start to form underneath. If this underground drainage system does form, it will speed up the development of karst formations there because more water will be able to flow through the region, giving it more erosive power.
In both cases mind that it's very hard for an ecosystem to form here, since there is poor to no access to some energy source. Typical ecosystems forming in such places rely on wastes being carried through flowing waters, nothing too big or fancy.
Same goes for their extension, they can hardly span over extended areas.