Q:

Why were those creatures on the space ship in The Force Awakens?

I was distracted by my kids at the time, so maybe I missed something....

 Han Solo was about to be shot by his angry employers, when suddenly these gigantic slavering tentacled monsters appeared and started eating people.

Where did THEY come from? Were they already on the ship?

A:

They were being smuggled by Han and Chewie.
The creatures are 

 Rathtars, monsters with tentacles and a circular mouth with teeth all the way round. They are considered deadly - Finn mentions the "Rathtar massacre".

The creatures were sought by some third party. Han captured them and was then smuggling them for the third party. I presumed they were for use by someone like Jabba in the way that he had a Rancor.
Since Episode VI Han has gone back to his smuggling past to make his money. 
They are set loose accidentally by Finn and Rey when Rey is trying to trap the Mercenary groups by sealing the doors.

A:

Alan Dean Foster's novelization clarifies what those creatures were:

“What’s a rathtar?” Rey asked Finn. ...
  It was Han who replied first. “You want the scientific description? They’re big and dangerous and ugly.”
  “O-kaaay,” she responded. “Why would anyone want something big, dangerous, and ugly? Who would want something big, dangerous, and ugly? And be willing to pay for it?”
  Where the hell was that accessway? Han wondered. Girl sure had a lot of questions. “People have funny hobbies,” he explained as he kept moving fast. “Some are collectors. There are those who collect different kinds of galactic currencies, some who collect old liquor containers, a few who like to accumulate holos of famous entertainers. Seems like the more money they have, the bigger the things they like to collect. There are even a handful who like to collect biological specimens. Those with money collect live ones. Those without money become scientists.” He gestured and they turned a corner.
  Finn moved closer to Rey. “I know of a perfect example that explains everything you’d ever want to know about rathtars.” She eyed him expectantly. “Ever hear of the Trillia Massacre?”
  She shook her head. “No.”

and to explain them on board:

“So,” she continued, turning her attention back to Han, who at least seemed willing to explicate a little, “you’re carrying these rathtars to a collector?”
  He nodded. “I got three going to King Prana. Kings not only like to collect, they like to boast about their collections. Seems Prana’s in competition with the regent of the Mol’leaj system. The regent doesn’t have a rathtar in his private zoo. Neither does anybody else.”
  “There’s a reason for that,” Finn muttered.
  “So I got this contract to get some for Prana. Three. It was difficult work. I’m expecting a bonus, and I’m not ready to give it all up just because of the Guavian Death Gang.”
  “Three!” Finn could hardly believe what he was hearing. “How’d you get them on board?”
  Han looked over at him. “I could tell you that Chewie and I got a bunch of their favorite food, tied it to a stick, and led them into the holding bay. But that would be a lie. Let’s just say I used to have a bigger crew.”

