WASHINGTON — Overruling two important precedents about the First Amendment rights of corporations, a bitterly divided Supreme Court on Thursday ruled that the government may not ban political spending by corporations in candidate elections.

The 5-to-4 decision was a vindication, the majority said, of the First Amendment’s most basic free speech principle — that the government has no business regulating political speech. The dissenters said that allowing corporate money to flood the political marketplace would corrupt democracy.

The ruling represented a sharp doctrinal shift, and it will have major political and practical consequences. Specialists in campaign finance law said they expected the decision to reshape the way elections were conducted. Though the decision does not directly address them, its logic also applies to the labor unions that are often at political odds with big business.

The decision will be felt most immediately in the coming midterm elections, given that it comes just two days after Democrats lost a filibuster-proof majority in the Senate and as popular discontent over government bailouts and corporate bonuses continues to boil.