It is illegal for foreign organizations or individuals to purchase ads to influence political campaigns. Social media, like Facebook, derive their revenue from advertisers. Advertising on social media has advantages over other media such as television. The social media enables advertisers to identify likely targets for their ads which makes them more cost effective than ads that are not targeted to an identified audience. It is also easier to disguise the identity of those who purchase ads on Facebook and other social media outlets.
Facebook determined that a Russian troll farm, located in St. Petersburg, called the Internet Research Agency, had spent $100,000 on Facebook ads during the 2015 and 2016 election cycle. The agency is financially supported by a wealthy individual with a personal relationship to Putin and Russian intelligence agencies. They followed the pattern that advertisers typically employ. During the 2015 campaign they tracked Facebook users who responded to divisive cultural issues such as race, immigration, gun control etc. They then targeted those users with divisive ads during the 2016 election cycle. They also focused their ads on geographic areas much like political campaigns do during an election. Facebook did not comment on which candidate was helped by the ads. However, it is pretty clear that Trump's campaign made extensive use of divisive cultural messages.
Facebook did not reveal any connections between the Russian troll farm and the Trump campaign, However, Trump's campaign obviously had lots of information that might have been useful to the Russian troll farm. Trump's digital media operation was managed by his son-in-law Jared Kushner. His operation also hired a consulting firm which uses sophisticated analytic tools to inform digital marketing efforts.