Comcast is buying online ad technology firm FreeWheel Media for about $320 million in a move that supports both its media content and cable businesses.