From the Congressional Research Service:
Each new House elects a Speaker by roll call vote when it first convenes. Customarily, the conference of each major party nominates a candidate whose name is placed in nomination. Members normally vote for the candidate of their own party conference, but may vote for any individual, whether nominated or not. To be elected, a candidate must receive an absolute majority of all the votes cast for individuals. This number may be less than a majority (now 218) of the full membership of the House, because of vacancies, absentees, or Members voting “present.”
This report provides data on elections of the Speaker in each Congress since 1913, when the House first reached its present size of 435 Members. During that period (63rd through 113th Congresses), a Speaker was elected four times with the votes of less than a majority of the full membership.
If a Speaker dies or resigns during a Congress, the House immediately elects a new one. Four such elections have been necessary since 1913. In the earlier two cases, the House elected the new Speaker by resolution; in the more recent two, the body used the same procedure as at the outset of a Congress.
If no candidate receives the requisite majority, the roll call is repeated until a Speaker is elected. Since 1913, this procedure has been necessary only in 1923, when nine ballots were required before a Speaker was elected.
From 1913 through 1943, it usually happened that some Members voted for candidates other than those of the two major parties. The candidates in question were usually those representing the “progressive” group (reformers originally associated with the Republican party), and in some Congresses, their names were formally placed in nomination on behalf of that group. From 1943 through 1995, only the nominated Republican and Democratic candidates received votes, representing the culmination of the establishment of an exclusively two-party system at the national level.
In six of the nine elections since 1997 (105th, 107th-109th, 112th, and 113th Congresses), however, some Members voted for Members of their own party other than the party nominees. Also, some Members in 1997 and in 2013 voted for candidates who were not then Members of the House. Although the Constitution does not so require, the Speaker has always been a Member. Further, in 2001, a Member affiliated with one major party voted for the nominee of the other. Until then, House practice had long taken for granted that voting for Speaker was demonstrative of party affiliation in the House
At
The Washington Post, Jeffrey A. Jenkins and Charles Stewart III tell what happened in 1923:
The 1922 midterm elections had reduced the number of Republicans in the House to 225 out of 435. If we count the number of Progressive Republicans at 24 — the number who voted in the caucus against re-nominating Frederick Gillett (Mass.) — then the Progressives could determine whether Gillett would have a House majority. And, while it was unlikely the Progressives would join with the Democrats to organize the chamber, anyone who could add knew that these 24 Republicans plus the chamber’s 207 Democrats also constituted a majority.
On the eve of convening the 68th Congress, Republicans held a nominating caucus at which the Progressive-Conservative split was revealed, when most of the 24 Progressives voted for Henry Cooper (Wis.). The Progressive bloc refused to abide by the cardinal rule of the binding nominating caucus, which required them to support Gillett as the price of participating in it. Instead, they held out for the passage of rules changes that would share parliamentary power in the House more equally. The Republican leadership initially refused, but soon realized that the Progressives meant business. The Progressives forced eight speakership ballots over two days, before Nicholas Longworth (Ohio), the Republican Majority Leader, capitulated and cut a deal — guaranteeing procedural freedom that would allow for liberalizing rules changes, in exchange for the Progressives standing down. Progressive leaders agreed, and Gillett was elected on the first ballot of the third day, and 9th ballot overall.
The Republican Old Guard had to swallow a lot to make this deal, but they had no choice — the alternatives were an unorganized House or a House organized along Democratic/Progressive lines.