You have already got the View.php file, that's good, to make this Q/A useful to others, I will also list the folder where you can get this file from (Magento 1.7): /app/code/core/Mage/Catalog/Block/Product/View.php
You are also right about the logic there to get category, the reason you are getting only one category is because the "break", which ends execution of "foreach" right after its first run. See: http://www.php.net/manual/en/control-structures.break.php
Let's change your code a little bit:
Get all categories:
$categoryCollection = $product->getCategoryCollection();
foreach ($categoryCollection as $category) {
$topCategory = Mage::getModel('catalog/category')->load($category->getId());
$keyword .= ' '.$topCategory->getName();
}
$headBlock->setKeywords($keyword);
Get 3 categories:
$i = 0;
$categoryCollection = $product->getCategoryCollection();
foreach ($categoryCollection as $category) {
if($i++ == 3){break;}
$topCategory = Mage::getModel('catalog/category')->load($category->getId());
$keyword .= ' '.$topCategory->getName();
}
$headBlock->setKeywords($keyword);