In the presidential elections of the United States, the electoral college system means that only the winner of a state receives any benefits from it (i.e. electoral votes). If a campaign wins 51% of the vote in a state, it receives all of that state's electoral votes; it gains no benefit from receiving additional votes above the margin necessary to win (this is true of 48 of the 50 states; the two exceptions, Maine and Nebraska, are explained below). This fact produces a very particular set of circumstances that explains the existence of swing states.
Since the campaign is interested in electoral votes, not popular votes, it tends to ignore states that it believes it will win easily; since it will win these without any effort, any effort put into them is essentially wasted. A similar logic dictates that the campaign avoid putting any effort into states that it knows it will lose. For instance, a Republican candidate (the more conservative of the two major parties) can expect to easily win Texas, which is a state that has a historically very conservative culture and a history of voting for Republican candidates; similarly, the same candidate can expect to lose Vermont, no matter how much campaigning he does in that state. The only states that the campaign would target to spend time, money, and energy in are those that could be won by either candidate. These are the swing states. Only two states--Maine and Nebraska--violate this rule. They both have a slightly more complicated system for allocating their electoral votes. Under their system, two electoral votes go to the person who wins a plurality in the state, and a candidate gets one electoral vote for each congressional district in which they receive a plurality. For example: Maine has two congressional districts. Of the votes in District 1, Jones gets 20 and Smith gets 15. In District 2, Jones gets 3 and Smith gets 32. In this situation, Smith would receive three of Maine's electoral votes, with Jones getting the fourth.
The actual procedures for deciding which states are swing states in any particular election varies across campaigns and across disciplines. Many political scientists use historical voting patterns: the more often a state has been won by candidates of one party in the past, the more likely it is to vote for that party in the future. Other factors that can help determine which states are swing states are:
The swing states of Illinois and New York were key to the outcome of the 1888 election. The (then) swing states of Illinois and Texas were key to the outcome of the 1960 Presidential election. Florida's status as a swing state was key to the outcome of the 2000 Presidential election.