First things first: jQuery (or its alias $) is a function. This is very important because functions are "first-class" objects in JavaScript. And since they are objects themselves, they have the ability to be given properties and methods just as any other object. For example:
var f = function() {};
f.h = function(x) {
console.log(x);
};
This is what allows jQuery to work its magic. In addition, through the use of inheritance, we have the potential to chain methods like you show in your first example. $(selector) returns a jQuery "interface" (technically [object Object]) based on the value of selector which, off of which, we can run methods such as .html, .css, and .toggle to name a few.