This is extensively addressed in the Star Trek: The Next Generation Technical Manual. In brief, Starfleet vessels typically have plans to convert void spaces (like shuttlebays and cargo holds) into 'triage' facilities when significant medical emergencies are encountered.
Large numbers of patients can be handled by emergency
conversion of one or more shuttlebays into triage and treatment centers. The main shuttlebay is equipped with five portable emergency hospital modules, which can be set up in the flight deck area, providing up to five triage and surgery wards. Three additional emergency patient care modules can provide up to seventy-five intensive-care beds and 530 medium-care beds.
Shuttlebays 2 and 3 are each equipped with one hospital and one emergency patient care module. These emergency care facilities are equipped for full biohazard protocol, minimizing exposure risk to Enterprise personnel.
Additionally, Shuttlebay 3 includes hardware for short-term conversion to Class H, K, or L environmental conditions, intended for nonhumanoid populations. Note that the use of shuttlebay facilities for medical service will necessarily impact shuttlecraft launch and recovery operations, a factor that can be significant during evacuation scenarios. For this reason, large-scale evacuation involving shuttlecraft support will generally make use of sickbay and other facilities first, before shuttlebay conversion procedures are invoked. Fewer numbers of patients can be handled by conversion of other facilities. Guest quarters on Decks 5 and 6 are
convertible to medical intensive-care use, and utility hookups
They give a scenario where overflow facilities (including additional areas of the ship and cross-trained personnel) are used to deal with a catastrophic emergency involving more than a hundred injuries. Triage is performed at the site of the accident, then again on the ship to determine which of the injured require immediate surgical intervention. The walking wounded and not-immediately-dying are then shuffled off to various treatment teams around the ship.
Using all personnel transporters aboard the Enterprise, a maximum of
approximately one thousand individuals per hour can be evacuated to
the ship. If the number of casualties is relatively small,
site-to-site transport can be used to beam the patients directly to
the on-board treatment area. Otherwise, patients are beamed only to
the transporter rooms and then shuttled to the treatment area by
gurney. This is because site-to-site transport effectively halves the
capacity of the trans- porter system. While on-site triage is
underway, conversion of secondary treatment areas would be prepared,
using medical conversion Kits. For major disasters, hospital and
emergency patient care modules can be deployed, providing full-scale
surgical and intensive-care facilities. If necessary, these
conversions can include complete biohazard protocols. Once patients
are received onboard, treatment teams would include all available
medical staff. The medical staff would be supplemented as needed by
additional cross-trained personnel from other departments.
We see this happening in TNG: Ethics (with shuttlebays converted into triage zones).
DATA: The Denver's standard crew complement is twenty three, but they were transporting five hundred seventeen colonists to the Beloti sector.
CRUSHER: I'll need to convert all three shuttlebays to emergency triage centres. I also want all civilians with medical training to report for duty.
And TNG: Disaster (where they use Ten Forward as their emergency medical locale).
DATA: I have surveyed all the turbolifts and service crawlways on this deck. Access to the Bridge has been completely severed by
emergency bulkheads.
WORF: Sickbay?
DATA: Heavy damage to section twenty three A has cut off access to Sickbay. I have ordered a security team to bring casualties here
until further notice.
A similar set of circumstances occurs on the Voyager where they tend to use the largest available space (the mess hall) in medical emergencies.
JANEWAY: Casualties?
TUVOK: Reports are coming in. Twelve wounded, many of them critically. The Doctor is setting up a triage facility in the mess hall. Two crew members were killed in the breach.
VOY: Year of Hell
On Voyager they also use the holodecks for triage.
TUVOK: Fifteen crew members have been seriously wounded with plasma burns, twenty seven experienced other injuries. The Doctor is setting up triage facilities in Sickbay and Holodeck two.
VOY: Deadlock
It's also worth mentioning that there are medical facilities on the Enterprise-D that we just don't get to see often, including a whole 'secondary sickbay'.
The Medical department, under the direction of the Chief Medical Officer, is principally located in two sickbay facilities on Deck 12. The primary facility, located on the port side of the ship, consists of two medical intensive-care wards, an attached laboratory, the CMO's office, and a small nursery. The second facility, located on the starboard side of Deck 12, is similar to the primary sickbay but features two dedicated surgery suites, a physical therapy facility, a nursery, and a null-grav therapy ward. Adjacent to the second facility is a dental care office and a full biohazard isolation unit.
The Star Trek: The Next Generation USS Enterprise NCC-1701-D Blueprints highlights that what we think of as 'the sickbay' is actually just the reception area for a much bigger (and largely unseen) facility that takes up a big chunk of Deck 12.
On a related note, we also have on-screen mentions of wards, treatment centres, recovery rooms, etc that we only see fleetingly or just in conversation. While this doesn't necessarily answer your question (e.g. what happens in an emergency?) it does help us to build a picture of a much larger set of facilities that aren't always visible to audiences.
CRUSHER: Tell Doctor Selar she can use ward three for the ambulatory cases, and I'll stay here.
TNG: Tapestry
and
INT. WORF'S SICKBAY ROOM
Worf has been moved to a private room just off main sickbay. There are several monitors, some medical equipment, and a diagnostic bed. Worf is sitting up in bed and has swung his legs over the side. His face is a mask of determination as he carefully grips the side of the bed and moves his feet down to the floor.
Script - TNG: Ethics