I'm surprised that the Allies managed to form a large force of their former enemy to fight with them. How come the transition was almost seamless?
I definitely wouldn't call transition of the Italian Army into a pro-allied force seamless, as much of the Italian army still remaining in 1943 pledged their loyalty to the Italian Social Republic (RSI), a German puppet state propped up in the aftermath of Italy's surrender and Mussolini's rescue from imprisonment. Italy found itself in a civil war almost immediately after the surrender in September of '43, and Italian loyalties were split between the government of Pietro Badoglio and the King, and Mussolini's RSI with German backing. Moreover, the Allied Powers were initially distrustful of Italian soldiers fighting alongside them in the first months of the Italian campaign, worried that there would still be pro-Fascist soldiers within the ranks of the Co-Belligerent army.
What led to the Co-Belligerent army's formation was a swift German invasion of the Italian peninsula not even 10 days after Italy's surrender. The surrendered army was mostly disarmed by this point, let alone having any sort of defensive garrison on the Italian/German border, which led to a rapid takeover of much of the peninsula. Germany installed the Italian Social Republic in the lands they did capture down to Naples and reinstalled Mussolini to power after the Gran Sasso raid, seeing him rescued from his imprisonment after a vote a no confidence ousted him from power.
This German invasion understandably split Italian loyalties into a pro-fascist and pro-monarchy camp now that the respective governments were divorced from each other. Italians officers and soldiers were forced to pick their loyalties and commanders were split between the two. Field Marshals like Pietro Badoglio and Giovanni Messe pledged their loyalty to the king while Marshal Rodolfo Graziani pledged loyalty to the RSI and Mussolini. Other field marshals and officers who were captured by the RSI and uncooperative with the Fascist government, such as Marshal Emilio de Bono, were simply shot by firing squad for loyalty to the King. In the end, around 300,000 Italian soldiers pledged loyalty to Mussolini's Fascist government, while ~250,000 pledged loyalty to the Monarchy and became the Co-Belligerent Army fighting alongside the Allies.
As for the formation of the Co-Belligerent Army, Germany's invasion of Italy after its surrender to the Allies inevitably led to the Monarchy's government cooperating with the Allies to drive the Germans out of Italy. What little remained of the Italian army under the King's loyalty was placed under the leadership and organization of the US Fifth Army which already landed in Southern Italy in early September. The pro-Monarchy forces that evaded German capture and were ready to fight with the Allies numbered less than 6,000 men and ~300 officers. The Allies were initially skeptical and distrustful of fighting alongside forces previously loyal to their former enemy, but certainly didn't refuse help from them. After pro-Allied Italian forces scored a victory fighting alongside the US at the Battle of San Pietro in December of '43, much of that distrust evaporated with US officers such as Mark Clark commending the Italians bravery in the battle.
Over the next few months of the Italian campaign, Italian co-belligerent units, with their numbers increasing as the war went on and more Italians took up arms in support for the Monarchy, were outfitted with US and British infantry equipment and other supplies, leaving them overall better equipped and more battle-ready than the Fascist government's units ever were during WWII with their shortages of all manners of equipment, fuel, and supplies. With proper outfitting and trust established between the pro-Monarchy Italians and Allied soldiers, Co-Belligerent units were incorporated within US and British army compositions and took the fight home to drive Germany out of Italy.
For further reading, I'll definitely say check out The Regio Esercito in Co-Belligerency, October 1943–April 1945 by Richard Carrier and The Italian Regio Esercito Co-Belligerent Soldiering, 1943–1945: A Grassroots Perspective by Nicolo da Lio, both co-writers for Italy and the Second World War: Alternative Perspectives
/u/quiaudetvincet 's answer is very thorough and highlights the pro-monarchy core of officers that made-up much of the Co-belligerent military's leadership. I would just like to highlight the role that the harsh conduct of the Germans after what they regarded as a betrayal on the part of the Italians played in inspiring Italians to resist the Germans. Geography was also a major factor. For those in the North of the country or in Axis occupied areas of the Balkans, they mostly faced the prospect of the ultimatum imposed by the Germans. For those in the South, they faced a much smaller number of Germans and were able to maintain their unit cohesion and quickly became part of the new Co-Belligerent Army.
As early as May 1943, the Germans began planning for the possibility of the collapse of the Mussolini government. The original plan was a series of interrelated operations to secure areas under Italian occupation. The occupation of Italy itself was fittingly known as "Operation Alaric." However, in the immediate aftermath of the Allied capture of Sicily (when one would think the Germans would be most concerned about the stability of the Italian government) Hitler, who had long feared an Italian collapse, believed that the 25 July meeting of the Grand Council of Fascism would actually reinforce Mussolini's control over the situation.
One of the major themes of the Fascist Italian leadership after the various military disasters in Greece, East Africa, North Africa and Sicily is the growing despondency of Mussolini. Something that was physically apparent by his increasingly gaunt appearance and noticeable weight lose. Prior to the events of July 25, Hitler had actually believed to have boasted Mussolini's spirits. Therefore the ouster of Mussolini actually took the Germans somewhat off-guard. However, the Germans were fortunate that the new government initially maintained that they would continue the war. A stance that convinced precisely no one, especially not the Germans. Seeing an Italian exit was imminent, the Germans decided to update the various planned contingency operations, including Alaric, into a single "Operation Axis."
The Germans moved quickly after the armistice. Surrounded Italian military units were typically given three options. To continue the fight alongside Germany, to surrender and be disarmed or to be overcome by force. With a lack of clear orders from Marshall Badoglio or Army Chief of Staff Vittorio Ambrosio, local commanders were often taken completely by surprise by the situation and had no clear course of action. Many fruitlessly attempted to contact their leadership and at best only received vague orders to neither surrender their arms or attack the Germans (unless attacked first.) Consequently, many military units (especially those in Italy proper) simply dissolved, with soldiers either making their way home or joining partisan bands. Some resisted successfully as was the case with Italian forces in Corsica and Sardinia. Others that resisted faced massacre by the Germans. Famously, 5,000 soldiers of the 33rd Infantry Division Acqui were executed by the Germans following two weeks of fighting. It was one of the largest mass executions of POWs during the war.
Most Italian soldiers disarmed by the Germans were opposed to continuing the fight and therefore over 700,000 Italian soldiers were interned by the Germans and deported to Germany. In an effort to avoid granting the Italian soldiers the rights guaranteed under the Geneva convention, most were classified by the Germans as "military internees" as opposed to "Prisoners of War" and subjected to brutality second only to that faced by Soviet POWs in German custody.
News of the harsh measures taken by the Germans against their comrades made their way to Italian military units that had yet to face the ultimatum imposed by the Germans. For the soldiers of the Italian 4th Army, which was making its way to Italy from France when the Armistice was announced, news of the German actions against Italian soldiers resulted in the complete dissolution of the Army in the region of the Franco-Italian border. Many soldiers tried to make their way to their homes, some offered brief resistance and still others took to the mountains to join the growing resistance movement.
Compare the plight of the 4th Army to that of the 7th Army. While the 4th Army was in Northern Italy at the time of the armistice, the 7th was stationed in Southern Italy in Calabria and Apulia. As the German forces in the region were no where near as expansive as those in the North, as well as the allied landings nearby allowed the Italian forces to maintain a chain of command and unit cohesion. The result was the successful resistance by much of the 7th Army against the Germans with the Italian units going so far as to preemptively attack the Germans.
In regions where the Italians outnumbered the Germans or where the Germans were unable to reinforce their position, we see the Italian units able to maintain their composure and successfully resist. Many of these units would form the core of what would quickly become the Co-belligerent army. The Legnano Division, which was half-way through a redeployment from France to Southern Italy is a case study on what happened to the Italian Army as a whole following the armistice. Representing a perfect example of the different circumstances faced by Italian troops in Southern Italy than those located in Northern Italy and areas under occupation. The units that had already been transferred to the South quickly pledged their loyalty to the King and became the core of the initial formation of the new Co-Belligerent Army. Whereas those that were in Bologna awaiting transport to the South at the time of the armistice faced the predicament of the German ultimatum.
TL;DR: The conditions imposed by the Germans on the Italian soldiers they detained were harsh and those that refused to continue the fight faced horrible internment and forced labor. In addition, the lack of an overwhelming number of Germans in some regions allowed Italian troops to maintain order and quickly accept the new status quo of Co-belligerence.