IIRC (though it’s been awhile since I watched Master and Commander) there was a scene where HMS Surprise escapes a storm but the masts were broken in the aftermath, and the entire crew was able to repair them and get them to function properly again as if they were brand new.
What tools were needed, and what was needed to be done for the broken masts to be able to be reattached?
Ships during the "golden age of sail" would normally carry a spare mast with them. For the most part, they would carry spares of everything they needed so they would be able to make those repairs anywhere. If you remember the scene in M&C far side, Capt Aubrey is talking with Mr Allen the sailing master about not having time to crawl threw the Brazilian rain forest looking for a new mast, and Mr Lamb will have to "bend the spare". Which means they would have to use the spare.
It would depend on the type of mast the particular ship had, as they normally had two types. Some had a single solid mast, and some had a two to three part mast. Also, it would depend on what section of the mast was damaged, the Topgallant (very top mast) the Top mast (Middle mast) or the Main mast (part from keel to deck and up to the main mast). It could be a simple as removing the broken section of the topgallant or topmast and replacing it, or in bad cases removing the entire mast including the mainmast. There where two basic methods of attaching the sail to the ship itself. There where deck, or keel stepped masts. The deck keeled basically attached to mast that ran down to the keel threw all decks, and the keel steeped was more of one piece to the top mast.
Keep in mind, everything was done by hand then. From shaping the mast, to attaching it, to lashing the mast parts together it was all by hand with hand tools. There would be a the Ships Carpenter and his Mates on board, all needing to know how to repair any part of the ship underway. This is just a quick explanation, but there are many many websites out there covering the age of sail.
An aspect of tall ships not covered in the other answers here is the fact that the masts consisted of multiple pieces (usually three): the lower mast, the top mast and the topgallant mast. The lower mast was attached directly to the keelson, which runs along the top of the keel (so technically it was not stepped to the keel although that gets the point across), as well as to strengthened deck beams supporting each of the decks. The top mast was attached to the top of this lower mast, and the topgallant mast was attached to the top of the top mast (here's a handy illustration of how this looks above-deck).
While the lower mast was placed in the ship during construction, and was usually repaired or replaced in dockyards with the lifting equipment available there, the top masts and topgallant masts were raised and lowered by the crew using solely shipboard equipment on a fairly routine basis (in extreme weather, for example, ships would lower the topgallants and sometimes the top masts).
So if the ship had received damage to a top mast or topgallant mast - and had spares aboard - then repair of the masts was easy (for the officers, anyway) to achieve. The topmast was erected by running a cable over the top of the lower mast and attached to the top of the top mast (which is lying flat on the deck). With the crew's muscle power leveraged by the capstan, the top mast was pulled up into position next to the lower mast, the cable was reattached to the bottom of the top mast, which was then lifted up into place with its lower part attached to the top of the lower mast. The same process was repeated (one mast up) to raise the topgallant into place.
Source: The Anatomy of Nelson's Ships, by C. Nepean Longridge.