In 1997, as a 19-year-old, Ayala started playing in the Mexican Baseball League as a relief pitcher. He spent five years with the Saraperos de Saltillo, where he was 10–3 with a 2.92 earned run average (ERA). in 65 games. He was signed as an undrafted free agent by the Colorado Rockies in 1999 and spent time with their A ball team before the Rockies sold him back to Saltillo in 2001.
In 2002, he was purchased from Saltillo by the Montreal Expos. He played with the AAA Ottawa Lynx in 2002. At the end of the 2002 season, he was signed by the Arizona Diamondbacks, but two months later returned to the Expos as a Rule 5 pick.
Ayala made his major league debut on March 31, 2003 for the Expos against the Atlanta Braves, working two innings of relief and allowing one run and four hits. His 10 relief wins that season were tied for 2nd most in the MLB. Ayala pitched well for the Expos/Nationals from 2003 through 2005. Over the three years, he averaged 70 appearances per year, and compiled an ERA of 2.75, and led the National League with 62 holds. In 2004 he ranked eighth in appearances with 81.
Ayala injured his elbow on the last pitch of a 2006 World Baseball Classic game. As a result, he underwent Tommy John surgery on March 30, 2006 and was knocked out for the entire 2006 season. Ayala began the 2007 season on the 60-day disabled list due to still recovering from the Tommy John surgery he previously had. After working eight rehab games in the minors, in which he allowed one run in 9-2/3 innings, on June 20, he was called up to the majors. He made his return with the Nationals on June 22, 2007, pitching 2/3 of an inning and giving up no runs and only one hit. For the season, he went 2–2 with 6 holds, one save, and a 3.19 ERA in 44 appearances.
In 2008, Ayala pitched in 62 games for the Nationals, going 1–8 with a 5.77 ERA.
On August 17, 2008, Ayala was traded to the New York Mets for a player to be named later. Ayala had requested the trade weeks earlier because he wanted a change of scenery, due to personal problems. The Mets sent Anderson Hernández to the Nats to complete the deal. On August 22, 2008, Ayala recorded his first save as a Met in a 3-0 victory over the Houston Astros.
In 19 games for the Mets, Ayala had a 5.50 ERA.
On February 18, 2009, Ayala signed a one-year deal with the Minnesota Twins. On June 22, Ayala was designated for assignment, and was released on July 2. According to Twins manager Ron Gardenhire, Ayala was released because he was unhappy with his role on the team. Gardenhire said of Ayala: "When you walk into my office and tell me you don't like your role, and he talked about his contract for next year, you lose me right there. I don't deal with that. We're talking about winning now. That's why he's out the door and another guy's in there to pitch. And it's not because he's a bad guy. His theories are a little different."
On July 3, 2009, he signed a minor league deal with the Florida Marlins. He was added to the major league roster on July 12. Ayala was designated for assignment on August 31. In 10 appearances, Ayala went 0–3 with an 11.74 ERA. After being DFA, Ayala said of the Marlins: "It was terrible what they did. I don’t know why they called me up if they were going to do this. I think it's a lack of respect. I know it's a business, but for me, it's something they’ve handled poorly."
Ayala signed a minor league contract with the Los Angeles Dodgers on December 16, 2009. He was assigned to the Triple-A Albuquerque Isotopes to start the season. He pitched in 14 games for the Isotopes, mostly as a closer. He was 1–3 with a 4.50 ERA and four saves. He was released by the Dodgers on May 17 after he exercised an opt-out clause in his contract.
On May 20, 2010, Ayala signed a minor league contract with the Arizona Diamondbacks. On July 16, 2010, Ayala was released by the Diamondbacks organization.
On August 27, 2010, Ayala signed a minor league contract with the Colorado Rockies. On November 22, 2010, Ayala became a minor league free agent.
On February 9, 2011, Ayala signed a minor league contract with the New York Yankees. On March 30 it was announced that Ayala had been added to the opening day roster.
On February 10, 2012, he signed a one-year major league deal with the Baltimore Orioles.
On October 29, the Orioles announced they intended to exercise their club option on Ayala worth $1 million. Ayala would've earned $1 million from the buyout had the Orioles not exercised the option.
On April 10, 2013, Ayala was traded to the Atlanta Braves for minor league prospect Chris Jones.
On February 7, 2014, Ayala signed a minor league deal with the Washington Nationals. The Nationals granted him his release on March 19, and he re-signed with the Orioles on March 20. He later exercised an opt out in his contract with the Orioles and signed a minor-league contract with the Toronto Blue Jays on June 6, 2014. He was activated by the Triple-A Buffalo Bisons on June 8, and released on July 4.
On June 29, 2016, Ayala was loaned to the Diablos Rojos del México of the Mexican Baseball League. He elected free agency on April 17, 2017.
On June 4, 2017, Ayala signed with the Saraperos de Saltillo of the Mexican Baseball League.