David has a degree of autonomy. He follows Weyland's orders but he has his own mindset and morality, or lack thereof. Ash was the same. He had a directive, but there was a lot going on in his machine mind and in Ian Holm's riveting performance.
Weyland ordered him to test the black material on a human subject. Holloway had already gotten on his bad side. The twain met and David was able to combine business with pleasure.
David also had a good guess that taking Weyland and co. to the Engineer could lead to their demise - something he was clearly okay with - but it was orders. Once again, the twain meets. He's a complex character.