It may be for various reasons
In television, cancellation refers to the termination of a program by
a network, typically because of low viewership, financial losses, or
unfavourable critical reviews. Other potential reasons for canceling
television programs include controversies involving the program's
cast, conflicts among the show's staff members or to make room for new
It also states that,
Commercial television is supported by advertising, or viewers paying
for TV-channels. Viewing figures are collected by audience measurement
ratings agencies (such as Nielsen in the United States), and the
programs with the highest viewing figures command a higher advertising
fee for the network. As such, shows with a low viewership are
generally not as profitable.
Other factors are considered as well, such as the cost to produce the
show.Very rarely are television programs cancelled for reasons other
than ratings or profitability. Four notable cases are Turn-On and
Australia's Naughtiest Home Videos, which were cancelled after viewer
and station outrage; Bridget Loves Bernie, forced off the air despite
high ratings because of threats of violence from Jewish radicals; Home
Run Derby, canceled due to the host's death; and Megan Wants a
Millionaire, which was axed in August 2009 following the arrest
warrant (and later, suicide) of one of the finalists.
Read full article here
So to sum up,Quoting
The show almost immediately flopped.
The show struggled in the ratings, and the studio wouldn't reduce the licensing fee.
The show struggled in the ratings and wasn't owned by the same corporation that owns its network.
The show had poor ratings and no syndication or streaming potential.
The show got poor ratings and was expensive.
The show got poor ratings, and nobody liked it (at the network).
The show just got too old.
Read more here
Basically if a TV show cannot produce money, doing bad at ratings, or if story is finished, show is older and hard to continue, actors think now the time to stop are the most common reasons for the cancellation or end of the series.
Cancellation and Ending of a program is different.
Read here for article about friends with Matthew Perry here
For Two and Half Men read here