It’s a scenario that has played out at companies for decades: Two colleagues meet. A romance blossoms. Complications arise.

The romantic relationship between Norfolk Southern Chief Executive Alan Shaw and the railroad’s chief legal officer is the latest in a long line of incidents that have tested corporate policies and caused headaches for companies internally.

Shaw was fired late Wednesday after a board probe found the relationship with Nabanita Chaterjee Nag violated the company’s policies. Nag was terminated as well. Neither responded to requests for comment.

Employment lawyers like to say that love and lust in the office isn’t illegal—it is just usually not a good idea. In the wake of the #MeToo movement , companies have tried to set clearer guidelines on relationships at work, though corporate advisers say many companies still struggle in how to effectively respond. Most employers restrict managers or senior executives from dating their subordinates, wanting to avoid power imbalances, but they are typically far more open to lower-level employees entering into relationships.

The reason? Many people meet partners in the office. A survey earlier this year by SHRM, an association for human resource managers, found that roughly 20% of workers have gone on a date with a co-worker in the past year.

“It’s always going to be there. This is where people meet,” said Deb Muller , chief executive of HR Acuity, a technology platform that helps companies track employee investigations and complaints.

There is little data to suggest relationships between superiors and subordinates are on the rise, though advisers say boards generally have less tolerance now for what they deem as poor executive behavior. When relationships are discovered, they are often swiftly dealt with.

McDonald’s in 2019 said it fired then-CEO Steve Easterbrook because of his consensual relationship with an employee. He wrote in an email to McDonald’s employees at the time that he had violated company policy on personal conduct.

BP earlier this year issued new rules related to workplace relationships, a move that followed the departure last year of CEO Bernard Looney , who resigned after the oil giant said he hadn’t been fully transparent about past personal relationships with colleagues.

Corporate policies for executives are more conservative than in the past, when some CEOs ended up marrying their secretaries, said Johnny C. Taylor Jr., chief executive of SHRM, the HR group.

If an executive dates an employee now, that could open a company to future litigation and other risks.

“There’s zero tolerance for the distraction and the reputational risk that you do get when you allow a CEO to date someone at work,” Taylor said. “There’s a question around judgment.”

Many boards, he said, are essentially telling CEOs: “We pay you enough money to find a date somewhere else.”

Workplace romances involving an executive are often poorly kept secrets internally, Muller of HR Acuity said, and may cause other employees to raise complaints of favoritism within the organization. “You cannot ignore the power play that could be in place,” she said.

For rank-and-file employees, companies have realized it isn’t practical to restrict consensual dating altogether. Some have tried to set clear guidelines on what is permissible. At Facebook parent Meta , for instance, employees must follow a simple rule: You can ask someone out but only once. The social-media giant has said it wants to prevent unwelcome flirting among colleagues.

“If you ask a co-worker out and that invitation does not result in a date, do not ask again,” Meta’s relationship guidelines state.

Meta also says colleagues can’t ask someone out if they have never met. “For example, it is not appropriate to find someone on the wiki and message or otherwise reach out to them seeking a date or letting them know you think they are attractive,” Meta’s rules state.

Like many employers, Meta also has requirements for when a relationship must be disclosed to HR—documents that are sometimes derisively referred to as “love contracts.” If a relationship goes south, there’s guidance there, too.

Should the relationship end, Meta notes, “you must remain professional to one another while at work.”

Write to Chip Cutter at chip.cutter@wsj.com and Lauren Weber at Lauren.Weber@wsj.com