Solution:
It is possible for all positive integers n≥3. To each pair of integers {i,j} with 1≤i<j≤n associate a prime number pij=pji in such a way that distinct pairs are associated with distinct primes.
Then define, for 1≤i≤n, ai as the product of all the prime numbers pij with i fixed and j ranging over all numbers from 1 to n different from i. Then the numbers ai satisfy the required properties. Indeed, the prime number pij divides only ai and aj, and therefore the greatest common divisor of ai and aj is equal to pij, while there is no prime number that divides three of the numbers a1,…,an.
The answer to the second question is 4. For n=4 one can take the numbers a1=2⋅3⋅5, a2=2⋅7⋅11, a3=3⋅7⋅13, a4=5⋅11⋅13. For n=5, however, it is not possible, and a fortiori it is not possible for n>5. Indeed, if a1,…,a5 satisfied the given conditions, then for each pair {ai,aj} there would be a prime number pij dividing both ai and aj, and all these prime numbers would have to be distinct. Therefore each number would have to be divisible by at least 4 distinct prime numbers, and each prime number would have to divide at most two of the numbers ai. In particular, the prime number 2 and the prime number 3 could each divide at most 2 of the numbers ai, so at least one of the ai would not be divisible by either 2 or 3. Such a number, being the product of at least four distinct prime numbers, would have to be greater than or equal to 5⋅7⋅11⋅13=5005, contradicting the hypothesis.