Companies tend to donate to political candidates so that those candidates, upon being elected, propose and vote in legislation that benefits those companies or vote out legislation that doesn’t (e.g. oil companies and climate change policies)
Edit: a detail that was implied but should be noted
See Citizens United v FEC. Basically, it was ruled that corporate lobbying counts as free speech under the 1st Amendment.